Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Manage Screen configuration in Android

In this article we will learn how to Manage Screen configuration of Android devices.

Handling screen configuration programmatically, locking the screen configuration, controlling the activity lifecycle when the device configuration changes are topics every Android developer works on when developing an application.

How to Manage configuration change manually?

The first obvious discussion point is how to manage a configuration change manually. When the configuration change is managed manually for an activity:

A new activity is not being created automatically when the configuration of a device changes. Normally upon configuration change, the current activity is destroyed automatically (the methods onPause(), onStop(), and onDestroy() are called) and a new activity is created (the methods onCreate(), onStart(), and onResume() are called) for the new configuration.

Any change to the GUI for the new configuration, if necessary, should be done manually.

Now let's consider what should be done programmatically to manage a configuration change manually. Suppose that inside your application you have an activity named InfoActivity for which you are going to programmatically manage the configuration change. Inside your AndroidManifest.xml, you already have an entry similar to the one below for InfoActivity:

Listing 1: Illustrates InfoActivity

<activity android:name=".InfoActivity" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity> 

As a first step you should add the attribute android:configChanges inside the activity tag.

Listing 2: Adding the attribute android:configChanges

<activity android:name=".InfoActivity"
    android:configChanges="orientation|keyboardHidden" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

The extra line tells Android that whenever the device configuration is changed or a hardware keyboard is opened, the implementation of InfoActivity has logic for handling the configuration change.

Next it's necessary to implement the logic for handling this configuration change manually. For this you need to override the method onConfigurationChanged() inside InfoActivity.

Listing 3: Overriding the method onConfigurationChanged() inside InfoActivity

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    .
    .
    .
}

The change in AndroidManifest.xml hinders the automatic creation of a new activity when the screen configuration of a device changes. Should any change be done to the GUI for the new configuration, this should be managed manually inside the overridden method onConfigurationChanged(). Even though these changes hinder creating a new activity every time the screen configuration is changed, they don't lock the configuration. In other words, visually you'll still see the screen rotating.

When to manage a configuration change manually?

The next question to consider is, when to manage a screen configuration change manually? The Android framework by default creates the displayed activity whenever the screen configuration changes. Is this always necessary? If not, in which cases is it necessary? The answer is- When the screen configuration changes, it's not always necessary to destroy the displayed activity and create it again for the new configuration. This is only necessary when the portrait and landscape configurations are inherently different. Let's consider two simple examples.

The first example below shows the days of a week in a listview. In both portrait and landscape configurations, the view is the same. Hence it's not necessary to destroy and create this activity again whenever the screen configuration changes.

Listing 4: Below shows the days of a week in a listview

<activity android:name=".Main"
    android:configChanges="orientation|keyboardHidden" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

public class Main extends ListActivity {
    private static final String[] MONTHS = new String[] {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, 
                android.R.configuration.simple_list_item_1, MONTHS));
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // .
        // Add code if needed
        // .
    }
}

The second example displays two images; in the portrait configuration, they are aligned vertically, and in the landscape configuration, they are aligned horizontally.

Portrait configuration, aligned vertically

Figure 1: Portrait configuration, aligned vertically.

Landscape configuration,  aligned horizontally

Figure 2: Landscape configuration, aligned horizontally

Clearly, the configurations are inherently different, and the corresponding activity needs to be created again every time the screen configuration changes. In this case, the activity lifecycle cannot be managed manually.

Listing 5: Activity creation after the screen configuration is changed

<activity android:name=".Main" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

public class Main extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getResources().getConfiguration().orientation ==
                    Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.configuration.main_p);
        } else {
            setContentView(R.configuration.main_l);
        }
    }
}

Locking the screen configuration

Programmatically locking the screen configuration requires to specifically tell Android which screen configuration it should display.

If you are not handling the screen configuration change manually, you should do this inside the implementation of the onCreate() method:

Listing 6: Illustrates onCreate() method

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    // or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    setRequestedOrientation(orientation);
    .
    // Add code if needed
    .
}

If you are handling the screen configuration change manually, you should specify the required screen configuration inside the implementation of the onConfigurationChanged() method:

Listing 7: Illustrates onConfigurationChanged() method

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    // or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    setRequestedOrientation(orientation);
    .
    // Add code if needed
    .
}

Alternatively, you can also lock the screen configuration statically. This is done in AndroidManifest.xml, in the activity tag, using the android:screenOrientation attribute.

Listing 8: Locking the screen configuration statically

<activity android:name=”.InfoActivity”
    android:screenOrientation="portrait" >
    .
    .
    .
</activity>

or
<activity android:name=".InfoActivity"
    android:screenOrientation="landscape" >
    .
    .
    .
</activity>

Conclusion

The above steps will help the developers to make the required configuration changes pertaining to the Android devices screen.



Software developer with more than 5 years of development on Java, HTML, CSS.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login