SuReSh PaTi
SuReSh PaTi

Reputation: 1361

How to fix orientation problems?

When i changed screen from portrait mode to land scape mode then automatically came out from my application.(same as landscape to portrait also)can any one tell me the solution for this..

this is my code for orientation,

if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(MyAlphabetsActivity.this, "ORIENTATION_PORTRAIT",
                    Toast.LENGTH_SHORT).show();
            setContentView(R.layout.portrait_main);
            System.out.println("int-----1--");
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.landscape_main);
            Toast.makeText(MyAlphabetsActivity.this, "ORIENTATION_LANDSCAPE",
                    Toast.LENGTH_SHORT).show();
            System.out.println("int-----2--");
        }

when change portrait to landscape control entered into else if() and display Toast massage also.......

Upvotes: 2

Views: 977

Answers (4)

Android
Android

Reputation: 2393

int o = getBaseContext().getResources().getConfiguration().orientation;
    if(o==1)//Portrait
    {
             Log.i("Portrait","=====");

    }
    else if(o==2)//Landscape
    {
             Log.i("Landscape","=====");
    }               

Upvotes: 0

Dany's
Dany's

Reputation: 928

You have two options: either you block the application in a single mode: portrait or landscape. Second option is to create two layouts: layout-land and layout-portrait It is not correct to have two different layouts. It just not work. You must have to folders in res folder. One folder layout-land in which you have your main.xml And a folder layout-portrait in which you have a file called also main.xml with handling the portrait mode.

Upvotes: 0

Lalit Poptani
Lalit Poptani

Reputation: 67286

If you have same layout in portrait as well as in the landscape mode then its better to stop the re-creation of Activity on rotation change by adding

android:configChanges="orientation|keyboardHidden"

attribute in your activity tag in the Manifest file.

Upvotes: 1

a.ch.
a.ch.

Reputation: 8380

You incorrectly handle the orientation change. You should read Handling Runtime Changes guide. Consider this as well.

Upvotes: 1

Related Questions