2

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.......

SuReSh PaTi
  • 1,373
  • 7
  • 28
  • 46

4 Answers4

1

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

a.ch.
  • 8,285
  • 5
  • 40
  • 53
1

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • i added line of code to manifest file,same problem...i added my orientation code please check it once ...i made mistake some where please help me – SuReSh PaTi Dec 14 '11 at 08:30
  • @Chiru check [this](http://stackoverflow.com/questions/7749675/how-to-stop-changing-the-orientation-when-a-progress-bar-is-spinning-in-android/7749727#7749727) answer. – Lalit Poptani Dec 14 '11 at 09:06
  • did you get the log when the orientation changes? – Lalit Poptani Dec 14 '11 at 10:19
  • no....when the application launch that time only got logs and toast massages ...either landscape or portrait – SuReSh PaTi Dec 14 '11 at 10:30
0

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.

Dany's
  • 943
  • 1
  • 7
  • 17
  • i created two folder and how to import layouts in two folder(R.layout.main)...please help me – SuReSh PaTi Dec 14 '11 at 09:04
  • you you dont need to import...it's just happening. You need to write setContentView(R.layout.main) and function of your orientation the app will know to look in the layout land folder or in the layout portrait folder. In these folders you need to copy the same main.xml file, and make some modifications of layout according to orientation. Anyway....they are not necessarly modifcations...layout may be exactly the same...just will not look good....you can try let it same in the first try. – Dany's Dec 14 '11 at 09:24
0
int o = getBaseContext().getResources().getConfiguration().orientation;
    if(o==1)//Portrait
    {
             Log.i("Portrait","=====");

    }
    else if(o==2)//Landscape
    {
             Log.i("Landscape","=====");
    }               
Android
  • 2,383
  • 1
  • 26
  • 44
  • thanks dude ....it's working fine....but it is taken only one layout means suppose we change portrait to landscape it's taken portrait and same as landscape – SuReSh PaTi Dec 14 '11 at 10:09