tablet/handset layout structure with fragments - android

In this article http://developer.android.com/guide/practices/tablets-and-handsets.html
we use res/layout/main.xml with one fragment for handsets and res/layout-large/main.xml with two fragments for tablets. We must check if second fragment is in the layout to define if app runs on tablet or on phone.
I have 4 layout (2 for phone and 2 for tablet):
layout-port
layout-land
layout-sw600dp-port
layout-sw600dp-land
I check screen orientation to define if display is in portrait or landscape mode and check if layout contains a fragment to define if it is tablet or phone.
Is there any better way to work with layouts and fragments?
Is it possible to use one layout if we have two fragments for example http://i.stack.imgur.com/FtzKs.png and if a phone display doesn't fit both of them to show only the first one?
Thanks in advance! :)

What you're showing there is indeed possible! That is something called a Master Detail Flow (if you're developing in Eclipse with adt, check out he new activity wizard, which provides this as a template option).
This layout is basically just two fragments inside an activity (or, now, as of android 4.2, they can be nested in another fragment as well!) that interact with each other in a certain way. To create the layouts you linked to, one would detect whether the device is a phone or a tablet, and then set the visibility of the two fragments in different situations accordingly.
You will find a number of methods for detecting screen size here, whether you want to use screen size in inches, pixels, or the manufacturers' default categories.
And properties like visibility and sizing can be set programmatically using Layout Params and its various subclasses.
In any particular case, whether you choose to use multiple layouts to support different screen sizes or to do more of it programmatically is up to you. Personally, I think that it is always a good practice to design your code modularly and then use layouts to put all the pieces together (it'll save you a lot of headaches down the line if you decide you want to change things up). But either way, supporting more devices will always require more code, and there are no two ways around that...one of the curses of android development :P

Related

Having multiple fragments at one screen with Jetpack Navigation component

Two fragments
I'm using navigation component and trying to understand is it possible to have two or more opened fragments simultaneously like mail app (one fragment is a list of mails, another fragment is detailed mail view). It's desirable to have them inside another container parent fragment.
Yes, it's even recommended to design your app for multiple screen sizes. You can do so by creating alternative layouts, e.g:
res/layout/fragment_items.xml # Screens smaller than 600dp available width
res/layout-w600dp/fragment_items.xml # Screens 600dp wide and bigger
where layout-w600dp/fragment_items.xml includes both fragments. This way you keep the same navigation destination and don't need to complicate things with conditional navigation.
It's desirable to have them inside another container parent fragment.
Depends. If you are going to use your list-fragment on multiple places, yes, because it would reduce duplicate code. If not, or your list-fragment is going to be different on multiple screens (you might add or hide some buttons) you don't need the container; and can just modify the fragment_items.xml layout for different screen sizes.

How to set different layouts for different orientations in a fragment

I have an activity which contain 3 fragments. 2 of them have data in a listview therefore when user rotate the screen everything is ok. but on my third fragment which contain buttons, when the user rotate the screen to landscape orientation,everything goes astray!
my third fragment in protrait orientation
my third fragment in landscape!
I have even tried to use scrollview but it's not working because when in landscape i don't want that big space at the middle. i wan't 3 x 2 buttons arrangment(i have 6 buttons.) when in landscape. and i think i can only achieve that by using 2 xml layouts for my fragment.
Question is how can i do that?
Make another layout folder layout-land under res and create landscape orientation layout file under this folder
After great help from sai Phani(see his comments above). i finally achieved my objective which was to create a landscape layout for my fragment by doing the following.
Create a folder under src\main\res and call it layout-land
copy your fragment layout xml (e.g peoplefragment.xml) which is in src\main\res\layout and paste it into src\main\res\layout-land folder. Don't change the file name!
Once you have pasted it, you can change the views alignment the way you would like them t appear on landscape. for my sutuation, i wanted them to appear like this in landscape
You may design your layouts that looks good for both portrait and landscape but you may customize for both orientation.
Look at SO link on the same issue # Android: alternate layout xml for landscape mode. The idea is simply to create folder names the correct way specified in Google documentation.
A good Google webpage is # Supporting Multiple Screens, search for "Using configuration qualifiers". There is a table stating Orientation and the folder names to use.

android: design code to dynamicaly select layouts

I want the same application to be delivered 2 different set of layouts. Ie the functionality is same but the graphics will be different for two different versions of the app. So i want to keep the same code and based of some variables want to decide which layout to be set for each activity. SO for each activity i will define two different layout.
This is my requirement. What is the best way to implement this. I can have an if else in each activity and define which layout to be set. Is that the right and best way. Please give your options on this
Take a look at this answer. It's about accessing a resource file from identifier, ie file name. You can do this with any type of resource (I think).
How to use getResource.getIdentifier() to get Layout?
Basically, you can do an if-else statement and assign the id of the layout you wish to use to a variable then load the layout using the identifier.
Actually there are many ways for ex you can change your layout based upon the orientation i.e landscape or portrait or you can change your layouts using languages for ex- you can create various folders for different languages.
Please explain your requirement briefly and if possible post some code also.
You can follow below links also.
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screendensities.html
Language Specific layout for android
http://www.c-sharpcorner.com/UploadFile/0e8478/supporting-different-languages-layouts-in-an-android-appli/

How to write a same android application for both phone and tablet devices?

I have two activities A and B in android application.On phone devices A launches into B.But I want to show both activities as fragments in the same activity on large tablets.
How to achieve this?Just writing different layouts won't work as logic will also change.So should I release two 'apks' for both phone and tablets?If not then how to write logic for both in the same application?
First of all, you need to modify your design to use a single activity and two fragments. The two fragments are the "master" and "detail" which can be reused in both the tablet and phone versions. So for example, you can make master.xml and detail.xml layouts. Then you create different activity layouts for different devices sizes and/or orientations. For example, you can have a default main.xml in the layout folder and a main.xml for tablets in the layout-large-land folder.
You can do this all in a single APK. I suggest that you google "master detail for android". This is the name for what you are trying to do.
Step 1: Create one class file, say MyActivity.java, write code to use phone fragment for phone and tablet fragment for tablet.
Step 2: Create two separate fragment classes, phoneFragment.java and tabletFragment.java which extends Fragment class.
Step 3: Create separate layouts for both fragments under layout directory, say phoneFragment.xml and tabletFragment.xml.
Step 4: Modify activity_main.xml to include both fragments.
And it's done!!
Here is an Example with full description.
You need to use fragments. They are part of the UI screen..Please watch the videos of the following link. They will help you a lot.
https://www.youtube.com/playlist?list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo

Button self "rearranging"

Wondering how I do build a layout with self rearranging button, when the user turns his device from portrait to landscape, using relativelayout ?
I do not want to use scrolling on my layout and I have a lot of buttons
You can provide two different main.xml (or any layout xml file) Place one in res/layout/ and one in res/layout-land/ When the device switches orientation the system will handle the "magic" of switching to the appropriate file.
Here is some documentation about supporting these type of behaviors...
If I'm understanding your question correctly what you are looking to do is have one layout displayed for portrait mode and another for landscape mode. This is actually fairly easy to accomplish.
Here is a link to a quick tutorial:
How to change your layout in Android landscape / portrait mode

Categories

Resources