Prash's Blog

How to set Android App orientation on startup April 21, 2012

Filed under: Android — prazjain @ 5:50 pm
Tags:

While coding one of the android app, i overwrote OnConfigurationChanged method to change the app orientation when user changes orientation. But there was a problem if the user starts the app in landscape mode, my app would by default still load in portrait mode. So add this little bit of code to check the orientation when you start your app and set the it accordingly.

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Configuration conf = this.getResources().getConfiguration();
    	if (conf.orientation== Configuration.ORIENTATION_LANDSCAPE)
    	{
    		setContentView(R.layout.main_landscape);	
    	}
    	else if (conf.orientation== Configuration.ORIENTATION_PORTRAIT)
    	{
    		setContentView(R.layout.main);
    	}
        
        InitUI();
    }
Advertisements
 

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