Single Activity with multiple fragments - android

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

Related

AndroidTV - create custom layout

I checked android docs, but didn't really find an answer on how to show custom layout in my application. I'm thinking of custom fragment placed next to navigation drawer. Maybe someone can give me pseudo code for better understanding? Thanks.
Visual representation:
http://imgur.com/a/VwTvs
On specific itemClick I want to make a layout (that contains icons/pictures) and place it next to nav drawer.
The key-word you are searching for is resource buckets. Essentially you can create resource-folders based on screen size, API version, orientation and such. Refer to the official documentation on how to create layouts for different screen sizes.
In Android Studio you can create resource-files based on those quantifiers by open the "new resource file" dialog and selecting the quantifiers you want in the lower part of the dialog.
Have a look here to read on layouts made exclusively for TVs.

Supporting different screen sizes with dynamic or static fragments?

I am found it confusing to be able to support multi screens using fragments. I thought the idea was that i could offer more than one version of the activity and that activity could have 1 or more fragments (depending on real estate) statically embedded.
Supporting dynamic fragments where these are added from code rather than embedded <fragment> element types. How would this be done in code as I wouldn't know the configuration details of the xml from the activity.
Does anyone have an example of this ?
When would I use static or dynamic fragments.
One reason I could foresee using dynamic fragments is to support the retaining on the fragment so the activity could just ask for it in code but I am finding it difficult to understand how this would be supported without the activity (java) knowing exactly what xml files are available
Thanks
I found it very useful to look through and understand the code of the Android Samples.
These sample projects are usually quite small and focus on only 1 or 2 main concepts each.
If you don't want to actually download and install the sample projects, you can also just browse the source code on GitHub.
For example in the NavigationDrawer sample the NavigationDrawerActivity class contains a dynamically created PlanetFragment class.
There are 2 main reasons to use a "dynamic" fragment:
You can pass parameters by code. For example, if you have a Fragment that displays an article, you create the Fragment by code and initialize it with the Article to display (using Fragment.setArguments(Bundle) ). This is the equivalent of passing parameters to an Activity using Intent.putExtras().
You can interchange different fragments in the same container.
Example 1: in your main Activity you have a navigation menu with 3 sections. When the user chooses a new section, you replace Fragment "Section A" with Fragment "Section B" in the same FrameLayout container.
Example 2: You display a two-pane layout on tablet (items list + details). Each time the user selects an item in the left list, you create a new Fragment initialized for the new item to display and replace the existing Fragment on the right with the new one.
Example 3: You have a ViewPager which displays a long list of articles. You swipe right or left to navigate to the previous/next article. These fragments will be created programmatically inside a FragmentPagerAdapter; you can't do that using static Fragments.
To answer your question on how to implement the dual pane with dynamic fragments, here is how:
Create a folder named values-land in your resources. In this folder, add a config.XML file in which you will have a code like this:
<resources>
<item type="bool" name="dual_pane">true</item>
</resources>
In the "normal" values folder, in the config.xml file, add this code:
<resources>
<item type="bool" name="dual_pane">false</item>
</resources>
In your activity, you can detect in the onCreate() the boolean value corresponding to landscape config you just created:
getResources().getBoolean(R.bool.dual_pane);
Then based on the boolean value, you can decide to inflate a layout with just two fragment containers on top of the other (single pane layout) or with two fragment containers next to each other (dual pane layout).
Finally add your fragments dynamically to the appropriate container and voila.
I like this approch because it is very Android like, by the use of the different values folder. Also you don't have to change much to an existing working Fragment pattern to make it support dual pane mode.

Use of NavigationDrawer for multiple Activities

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/

Android: how to create application frame controls

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.

Converting Existing App to use Tabs

I have an existing app that is using a Dashboard style pattern where there's a main menu, and clicking icons on the main menu drive start different activities... In order to navigate to a different function, you need to go back to the Dashboard menu and select another icon.
I want to change my application to a tabbed format similar to the one below with Icons:
What type of View is being used below? Is this a FragmentActivity? Also, what is the best approach to go about conversion? I have a bunch of layouts with listviews in linear/relative layouts... Will I be able to reuse any existing code? I want also to be able to use the solution with Android 2.1 and up if possible.
Thanks!
In the image you provided, it looks to be a TabHost that is used (which can be within a normal Activity or a Fragment Activity) and will be available for Android 2.1 and beyond when using the Android Support library. Based upon your description, you most likely have an Activity per each of your items that you will probably want to convert into a different Fragment. This may take a little time, but a Fragment is very similar to a normal activity in many ways, so once you start getting used to it, converting over the old Activities should be a breeze.
If you plan on using these tabs and you follow the Android design UI guidelines, you may want to use the TabHost in conjunction with a ViewPager. There is a great tutorial for this online that also allows for backward compatibility (down to at least 2.1) found here: http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/
Support library for fragments/viewpager: http://developer.android.com/tools/extras/support-library.html
More info about a TabHost and using Tabs with fragments can be found here:
http://developer.android.com/reference/android/widget/TabHost.html
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
You can use TabLayout and a TabActivity to achieve layout in picture. Bad news is these are deprecated as of Android 3.0. Google advises to use fragments instead of these.

Categories

Resources