How to detect orientation change in layout in Android?
Use the onConfigurationChanged method of Activity.
See the following code:
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
You also have to edit the appropriate element in your manifest file to include the android:configChanges
Just see the code below:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
NOTE: with Android 3.2 (API level 13) or higher, the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher, you must declare android:configChanges="orientation|screenSize" for API level 13 or higher.
Hope this will help you... :)
Android: Detect Orientation Changed
Ok, after trying to use the Android API and not being able to do what I need, I implemented my own algorithm and actually it wasn't that complicated:
I used a OrientationEventListener, and calculated if the orientation is in the 4 orientation points (in my code I only detect LANDSCAPE_RIGHT
and PORTRAIT_UP
:
orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
public void onOrientationChanged(int orientation) {
if(canShow(orientation)){
show();
} else if(canDismiss(orientation)){
dismiss();
}
}
};
@Override
public void onResume(){
super.onResume();
orientationListener.enable();
}
@Override
public void onPause(){
super.onPause();
orientationListener.disable();
}
private boolean isLandscape(int orientation){
return orientation >= (90 - THRESHOLD) && orientation <= (90 + THRESHOLD);
}
private boolean isPortrait(int orientation){
return (orientation >= (360 - THRESHOLD) && orientation <= 360) || (orientation >= 0 && orientation <= THRESHOLD);
}
public boolean canShow(int orientation){
return !visible && isLandscape(orientation);
}
public boolean canDismiss(int orientation){
return visible && !dismissing && isPortrait(orientation);
}
How to detect orientation change in layout in Flutter?
In order to determine the Orientation of the screen, we can use the OrientationBuilder
Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.
new OrientationBuilder(
builder: (context, orientation) {
return new GridView.count(
// Create a grid with 2 columns in portrait mode, or 3 columns in
// landscape mode.
crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
);
},
);
you can find the complete example here:
https://flutter.io/cookbook/design/orientation/
How to detect when the device switch from portrait to landscape mode?
See the official documentation http://developer.android.com/guide/topics/resources/runtime-changes.html
Changing it will actually create a new view and onCreate will be called again.
Furthermore you can check it via
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
Where to check for orientation change in an android fragment
Keep this check in onCreate(). When you rotate your device the app is restarted. So onCreate is called again when the app restarts.
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
// Landscape
}
else {
// Portrait
}
Check orientation on Android phone
The current configuration, as used to determine which resources to retrieve, is available from the Resources' Configuration
object:
getResources().getConfiguration().orientation;
You can check for orientation by looking at its value:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}
More information can be found in the Android Developer.
landscape layout does not change on orientation change and portain layout does not change in orientation change
Remove android:configChanges="orientation"
in your AndroidManifest.xml
section for this particular Activity
.
How can i detect orientation changes in portrait mode in android?
Use OrientationEventListener:
orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
public void onOrientationChanged(int orientation) {
//do something
}
};
Related Topics
Android Action_Image_Capture Intent
Java Finished With Non-Zero Exit Value 2 - Android Gradle
How to Run a Runnable Thread in Android At Defined Intervals
Repeat a Task With a Time Delay
"R Cannot Be Resolved to a Variable"
How to Display an Alert Dialog on Android
Can the Android Layout Folder Contain Subfolders
Service VS Intentservice in the Android Platform
J2Me/Android/Blackberry - Driving Directions, Route Between Two Locations
Navigationview Get/Find Header Layout
"Failed to Load Ad: 3" With Doubleclick
Difference Between Compilesdkversion and Targetsdkversion
Android Basics: Running Code in the Ui Thread
Android Intent Filter For a Particular File Extension
How to Convert a Base64 String into a Bitmap Image to Show It in a Imageview