Home > Tricks & Tips > The less-known over screen orientation

The less-known over screen orientation

I’ve just found a really interesting problem today at work, it’s about screen orientation.

I was trying to create a very simple application, which:

+ contains one button to change screen orientation state: PORTRAIT to LANDSCAPE, and vice versa.

+ the text on button determines the action going to be done if click. If the current is PORTRAIT then the text should be LANDSCAPE, and vice versa.

OK, so here is one very simple implementation, easy-read, easy-understood:

package pete.android.study;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    // the constant strings defining text to display on button
	static final String ORIENT_PORTRAIT = "Set Portrait";
	static final String ORIENT_LANDSCAPE = "Set Landscape";
	// determine screen orientation state changed
	boolean mState = false;
	// the main button
	Button mBtnSet = null;

    public void onCreate(Bundle savedInstanceState) {
        // load button from layout
        mBtnSet = (Button)findViewById(R.id.btnSet);
        // set default text display
        // handle click event
        mBtnSet.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// state FALSE: switch to LANDSCAPE
				if(!mState) {
				// state TRUE: switch to PORTRAIT
				else {
				// update state
				mState = !mState;

However, there has a problem inside. First, try to build and run it.

The Initial Screen

The Initial Screen

By default, the screen is set as PORTRAIT, and the text is “Set Landscape“, which is correct.

Next, try to click on button, this will happen:

After First Click on Button

After First Click on Button

The screen orientation is changed to LANDSCAPE, however, the text on button doesn’t change at all. That’s so surprising!

And keep clicking on button one more time:

The 2nd Click

The 2nd Click

Now, the text changes to “Set Portrait“, well, the orientation doesn’t change, but on the third click, the screen interface backs to the first initial state.

It’s interesting, isn’t it?

The question in mind is why it is…

First, reading this one on Google Android References – Activity#ConfigurationChanges

Reading clearly, you’ll find that, everytime the screen orientation changes, the Activity is auto destroyed and recreated again, which means the text “Set Landscape” is reset as the initial state, a re-call of onCreate() method.

That’s the problem!

The solution is very simple, by adding the attribute – android:configChanges=”orientation” – , it will avoid re-create the activity and keep the current state.

Note: if you always to handle screen orientation without attribute of handling orientation, sometimes you might get exception, or errors in your application as well, even crash-out and freeze. So always keep this in mind🙂.

Hope you having fun with this little tips!



Pete Houston




  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: