I'm developing an app for a client where the requirements for tablet vs phone form factor switches from tabs for the phone and a split view for larger tablet size devices.
Currently the app works on phone with the tabs and has a main activity that derives from TabActivity.
From what I've read, designing for different size devices generally involves different layouts for different size devices... but what if the activity's base class is different in each layout. (ie:TabActivity for phone, Activity for split view)
Since TabActivity has been deprecated for over two years, hopefully you weren't planning on using that anyway.
Hence, use modern tabs, such as a ViewPager, in conjunction with PagerTabStrip or the tab-style indicator from the ViewPagerIndicator library (personally, I prefer the look of the latter). Have your UI structured into two fragments. Put the fragments in the ViewPager on the phone. Put the fragments in two FrameLayouts in a horizontal LinearLayout on the tablet.
You also can use tabs in the action bar, or possibly a FragmentTabHost, in much the same fashion.
Related
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.
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
My application consists of tabs using Tabhost and Activity group. It is working well for now. However, I am told to extend the app to support tablet. The current app seems to work in tablet, with less space utilization. I am told to use split views in few screens.
As a result, I made a rigorous study on extending the design. One proposal is to use fragments. This eventually necessitates a major design change and consumes more time. The other one is to tweak the existing code to extend support which is far less recommended.
The app is aimed to support all Android versions above 2.2
Could anyone guide me a better solution?
EDIT:
I am already using new layout naming conventions.
layout-sw600dp
layout-sw800dp
For current smartphone version, say for instance, a screen has list view as
Item A
Item B
Item C
Tapping any item opens a new activity in the activity group showing the details.
But in tablet, the above two features are brought to a single layout using split view.
Item A |
Item B | Item details
Item C |
I am stuck with implementing the above feature. Can I use fragments inside ActivityGroup?
How to add multiple dynamic fragments in dual panel? I need to add Dual panel for Tablet and single pane for mobile. Also in Detail part having two fragments, if tablet it shows fragments side by side,else if mobile it shows fragments top and below.How to do this.
can i add another framelayout in dual panel? or In detail fragment,simply add linear layout?
Here's a link to android.com. There is quite a good tutorial how to support different layouts for tablets and smartphones. Basically you create to different layout folders (one for tablet, one for phone) and create the layout you want in them. Android will automatically select the right one.
http://developer.android.com/guide/practices/tablets-and-handsets.html
I have created an app using Fragments throughout, as per the guidelines, so that I can show more detailed views on larger screens. However I also want to use tabs for navigation, which as per the guidelines should also be fragments rather than the deprecated TabHost.
Some Googling has shown that you can't / shouldn't nest fragments. My question is what are you meant to do if you want to have tabs, but you also want to support fragments for different size devices? Should I be removing the fragments and making two separate activities for phones and tablets?
Making it more confusing, the design guidelines here, show tabbed navigation and then what looks like a fragment activity. Is this a case of nested fragments or are they separate activities?
There is an API demo a code sample that demonstrates combining a ViewPager and a TabHost to switch Fragments: Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager
If you don't like TabHost, here is another example of the same code but using ActionBar tabs and ActionBarSherlock: http://code.google.com/p/sherlock-demo/