Android Orientation Changes

When user of Android mobile devices changes its orientation in some situations we need to handle Android orientation changes programmatically. So here is how to handle runtime changes. In your brand new application, make changes in your manifest first.
app→manifests→AndroidManifest.xml

Andoid-Manifest-Changes-for-Orientation

In there, bellow
android:theme=”@style/AppTheme.NoActionBar”
type:
android:configChanges=”screenSize|orientation|keyboardHidden”>

android-configuration-changes

Then, go to MainActivity.java class and delete some of code.

configuration-changes-delete-code

And delete a bit more:

configuration-changes-delete-some-more-code

Now, paste following code in MainActiviti.java:

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

txtHello.setText(“Horizontal”);

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

txtHello.setText(“Vertical”);

}

}

Of course, you must have txtHello text view in front end of your application in order to make this code work.

whole-code-for-android-configuration-changes

In addition, it useful to know width and height of our device screen. Note, really important in programming is to know screen size in dp, not in physic pixels. The best practice is to make dedicated new class that will read mobile device screen with and height. So lets add new class to our Android studio project and let’s name it ScreenMetrics.

new-class-screenMetrics

We need to add following private members of our class:

private Activity activity;
private float dpWidth;
private float dpHeight;

and also one public constructor that takes activity as input parameter:

public ScreenMetrics(Activity activity){

this.activity = activity;

Display display = activity.getWindowManager().getDefaultDisplay();

DisplayMetrics outMetrics = new DisplayMetrics();

display.getMetrics(outMetrics);

float density = activity.getResources().getDisplayMetrics().density;

dpHeight = outMetrics.heightPixels/density;

dpWidth = outMetrics.widthPixels/density;

}
public float getWidth() { return dpWidth; }
public float getHeight() { return dpHeight; }

Whole code is:

ScreenMetrics-class

And we can use this class in our main class as following:

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

ScreenMetrics utility = new ScreenMetrics(this);

String output = “Width: ” + utility.getWidth() + ” Height ” + utility.getHeight();

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

txtHello.setText(“Horizontal ” + output);

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

txtHello.setText(“Vertical ” + output);

}

}

 

External links:

Android Orientation Changes on YouTube
Android Orientation Changes on mrbool.com
Android Orientation Changes on Stackoverflow
Android Orientation Changes on Stackoverflow

Posted in android Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*