Over a million developers have joined DZone.

Managing Multiple UI Layouts in a Single Activity



Due to the wide proliferation of smartphones, making application functionality (at least some functionality, if not all) available on a mobile is a must-do activity for enterprises. Most applications published for a mobile phone, have to face up to the challenge of presenting content on a limited space. Not only is the large size that was taken for granted on the desktop not available, application designers also have to contend with the fact that users will view the content from devices with varying sizes and resolutions. As there is limited space available on the mobile screen, it is practical to design applications using multiple screens, with each screen showing limited (and relevant) information in easy-to-consume chunks and arranged in a logical flow.

While developing applications using multiple screens is simple and straightforward, it creates some challenges. In this article, we would like share a mechanism that allows creation of applications with multiple screens, while overcoming some of its associated limitations. While the method described in the article refers to the Android platform, it may be possible to implement the same concept on the iOS, Windows Phone and BlackBerry platforms, if the platforms provide similar features.

Android Activity

On the Android platform, the Activity class is one of the central concepts for application development. An activity is usually associated with a user interface (UI) layout that is displayed when the activity is visible. With an activity playing a central role in an Android application, it would be interesting to look at the activity life-cycle, before continuing with the main topic of the article. Given below, for reference is the activity life-cycle, as published on the Android Developers website.

One UI per Activity

The standard practice for developing applications with multiple screens is to create one screen per activity. Whenever the user moves to a different screen of the application, the application transitions from the existing Activity to the new Activity and the transition is managed by the Android platform. As part of the transition, the 'onPause' method of the calling Activity ('parent') is invoked, before the 'onCreate' method of the called Activity is invoked, bringing it into focus.


Sharing Data

It is important to note that Activities are independent of each other and are represented as different classes in the application. If needed, paused Activities are destroyed by the Android platform and re-created as needed. As Activities are different classes and are created and destroyed as per the application flow (and platform need), an important consideration is sharing data between them. It is possible to pass information between Activities by storing it as 'extra' information attached to the Intent object, when an Activity is launched. It is also possible to store the information in a class derived from the 'Application' object, from where it can be accessed by all activities in the application. While only basic data types can be passed as 'extra' data, complex, user-defined data types can be stored inside the Application object.
Managing Multiple UIs in one Activity
While the standard and most common method of developing an application with multiple screens is to use one Activity per screen, it is possible to develop multi-screen applications using a single Activity. By using a single Activity to manage and display multiple layouts, it is possible to avoid Activity transitions. Additionally, application data can be stored in the Activity itself, without the need to use an Intent object or storing them in the Application object. The mechanism of developing a multi-screen application inside a single Activity is very simple. It is done by using the 'visibility' flag associated with each UI element. Let us go through an example to understand the concept.



We would like to illustrate the concept, using a simple example. The example presents the UI of the 'Time Quiz' application published by the author on the Google Play store. When started, the application displays the 'Main' screen, which allows the user to play. On starting the game, the 'Game' screen is displayed, which shows a clock face and four options, of which one of the correct answer. If the user selects the correction time, the 'Happy' screen is displayed. If the users answer is incorrect, the 'Sad' screen is displayed. All the four screens are managed inside a single activity, while the appearance of the screens is controlled by the 'visibility' flag. The four application screens are given below

Managing Visibility

Each of the screens is defined using a layout container. The layout XML is presented below. In the interest of brevity/space, some of the UI elements have not been included.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="horizontal|vertical">

<LinearLayout android:id="@+id/layoutMain" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:orientation="vertical" >

<Button android:id="@+id/btnPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="center"/>

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal">

<TextView android:id="@+id/textScore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="20sp"/>

<CheckBox android:id="@+id/checkBoxEnableSound" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="true" android:text="@string/str_sound"/>


<Button android:id="@+id/btnExit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_exit"/>


<LinearLayout android:id="@+id/layoutClock" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_gravity="center" android:visibility="gone">

<timequiz.ClockView android:id="@+id/viewClock" android:layout_width="220dp" android:layout_height="220dp" android:layout_gravity="center"/>

<TextView android:id="@+id/textRemainingTime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="20sp"/>

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center">

<Button android:id="@+id/btnOption1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<Button android:id="@+id/btnOption2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>


<Button android:id="@+id/btnBackMainClock" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_back_main"/>


<LinearLayout android:id="@+id/layoutHappy" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_gravity="center" android:visibility="gone">

<Button android:id="@+id/btnBackMainHappy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/>


<LinearLayout android:id="@+id/layoutSad" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_gravity="center" android:visibility="gone">

<Button android:id="@+id/btnBackMainSad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/>



  Initially, the 'visibility' flag for all containers, except the 'Main' screen is set to 'gone', making the screens invisible. When the user starts the game by pressing the clock face on the main screen, the 'Main' screen visibility is set to 'gone', hiding it and the 'Game' screen is made visible by setting its visibility flag to 'visible'. The visibility of a layout can be controlled using the setVisibility() method and using the values View.VISIBLE or View.GONE, as appropriate. If the user chooses the correct option, the 'Happy' screen is made visible and the 'Game' screen is hidden. If the user chooses the incorrect option, the 'Sad' screen is made visible and the 'Game' screen is hidden. On the 'Happy' and 'Sad' screens, when the user presses the image, control is transferred back to 'Main' screen, by making it visible and hiding the 'Happy' / 'Sad' screen, as appropriate.


Managing the 'Back' Event

When using one Activity per application screen, the developer does not need to worry too much about the 'Back' button. On the Android platform, the 'Back' button (physical or on screen) is used to navigate back to the parent activity, if the application has multiple activities. Else, control is returned back to the main screen. When developing an application with a single Activity, one needs to handle this event explicitly. For proper application behaviour, the 'onBackPressed' Activity method needs to be overloaded. On receiving the 'Back' event, we need to change the visibility of the screens as per application flow.


Screen Workflow Flexibility

When an application is modeled using multiple activities, the flow of control will be decided by the way the activities are called. In the example, when using multiple activities, flow of control will be 'Main' screen to 'Game' screen to 'Happy' or 'Sad' screen. When closing activities, the flow will be reversed. Thus, when the 'Happy' or 'Sad' Activity is closed, control will come to 'Game' screen. When the 'Game' screen is closed, control will go to 'Main' screen. Thus, it is not possible to go to the 'Main' screen directly from the 'Happy' or 'Sad' screen. By using a single Activity to manage multiple layouts, it is possible to go directly to the 'Main' screen from the 'Happy' or 'Sad' screen, as the flow of control is decided by the application and not managed by the Android platform.


In this article, we have shared a mechanism to create Android applications that support multiple screens that are supported by a single Activity. Using this mechanism, Activity transitions can be avoided and management of application data becomes easier. But, the complexity of code (due to the need to managed screen visibility) increases.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}