Home > Software engineering >  Single Activity with multiple fragments
Single Activity with multiple fragments

Time:03-17

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.

  1. The layout choice: I need a layout for full screen fragment screen 1 but another splitted one for other screens.

  2. 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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related