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.
Related
I m new to Android development and stuck with design. I d like to design a single activity application with multiple fragments. But whatever I consider, I m stuck with design.
I first considered fragments as components or widgets that I could organize where I wanted on my screen. But a single activity has only one xml view layout and can not find a solution.
Here is what I want.
UI design
The problems are the following.
The layout choice: I need a layout for full screen fragment screen 1 but another splitted one for other screens.
The keyboard: if I do a fullscreen fragment layout for all my screens with a (game-keyboard) fragment and (dico-keyboard) fragment, how do I remove the keyboard in dico fragment on tablets ? And can I share keyboard code between both fragments ?
I saw that calling setContentView during runtime is kinda annoying as you have to reassign your variables and is not suitable.
All the applications I find are fullscreen single fragment on layout allowing libs like ViewPager. I can not find an example of composed screen with fragment components.
What layout would you chose, with what kind of fragments ?
Or do you know a library that allow composing fragments as widgets on screen ?
Thanks for your help.
Welcome to Android. Buckle up, it's going to be a bumpy ride.
First, you should spend some quality time with the Android Developer Guides.
Developer Guides | Android Developers
The insight for your particular challenge is that layouts are resources, and and you can have a layout resource that changes based on the configuration.
So for example, you could have a resource like layout_main with one XML file for small devices and a different XML file for large devices.
App resources overview | Android Developers
Create alternate layouts | Support different screen sizes | Android Developers
You can arrange fragments on a layout using the Android Studio layout designer.
You can add Fragments and navigate.
First of all yu need a navigation: In the left hand side on Android Studio you can find the Resource Manager Then click to Navigation then click to + and choose the Navigation resource file
After this you can add fragment to the navigation:
New Destination (green +) (Don't forget to give name to your fragments).
After this you can add the navigation to your activity's xml. You should use the app:menu=”#menu/bottom_nav_menu” code in your xml
Here a video about the process:
https://www.youtube.com/watch?v=Chso6xrJ6aU
I have a requirement to use NavigationnDrawer in my project.
Question: considering my app has multiple Activities (some has master/detail fragments)
The master/detail fragment would have single container layout for tablets and large width devices and 2 different layouts on smaller width for master & detail views.
What can be right direction to proceed (1) or (2) or any different advice which makes more sense?
Every activity has a NavigationDrawer and it's layout rendered using setContentView(layourID) in OnCreate().
All activities extends a Base Activity which uses NavigationDrawer.
If answer is (2) please provide some more useful advice. And if it's something else , please do share .
I think this tutorial may help you. http://www.youtube.com/watch?v=K8hSIP2ha-g and also refer this http://www.tutecentral.com/android-custom-navigation-drawer/
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
We have an Android application made of menu of modules and each module has the same layout as on this picture:
You have a header with module name and two buttons on the bottom. The module itself is behind those three transparent elements. Left button allows the user to move to the previous screen and right button gets him back to the menu with modules. Text in header changes based on current screen action.
My question is how to build the module transparent frame. Is it possible to have one layout in Android on top of the other? Should we design the code as a parent class that would be inherited by all the modules? I am interested in some best practice. I have experience with Java but only theoretical knowledge of Android.
Prepare a separate common layout and use to add that layout to as many layouts you want.
for more details check this
What you are looking for are so called Fragments. These Fragments are sort of like Activities, with their own layouts and lifecycles, but you are able to use multiple Fragments at once. This means you'll be able to use one Fragment for your navigation and header, and one for the module behind it.
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.