Skip to content Skip to sidebar Skip to footer

Orientation Android

I want to add orientation on my app but I need that--> when my phone on PORTRAIT style works A activity and when I change PORTRAIT style as LANDSCAPE style A activity stops and

Solution 1:

do this

@OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screenif (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
       //here call activity A
    } elseif (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
       //here call activity B

    }
}

Solution 2:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{   

}
if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
{   

} 

Solution 3:

Start your Activity B with below code -

@OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screenif (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Intenta=newIntent(this, B.class);
        startActivity(a);
    } elseif (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        //do nothing
    }
  }

And, in your AndroidManifest.xml file's both activity tag. Just declare like below

<activityandroid:name=".A"android:configChanges="orientation|keyboardHidden"android:label="@string/app_name">

Solution 4:

First check the orientation this way:

Displaydisplay= ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
intorientation= display.getOrientation();

and start the Activity you wanted depending on the value of orientation.

then in the activity override this method:

publicvoidonConfigurationChanged(Configuration newConfig) {

//start the other activity
}

Post a Comment for "Orientation Android"