My problem:
For some requirements i need two different xml layouts for my activity:
- One for Landscape mode.
- And another one for Landscape-reverse mode (upside-down of Landscape).
Unfortunately Android doesn't allow creating a separate layout for landscape-reverse (like we can do for portrait and landscape with layout-land
and layout-port
).
AFAIK, the only way is to change the activity-xml from java code.
What i've tried:
1) Override onConfigurationChanged()
method to detect orientation changes, but i can't figure out if it's Landscape or Landscape-reverse:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d("TEST","Landscape");
}
}
( Whith android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"
in my activity tag in manifest)
2) Use an OrientationEventListener
with SENSOR_DELAY_NORMAL
as suggested in this answer but the device orientation changes before entering my if
blocks, so i get a delayed update of the view:
mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
@Override
public void onOrientationChanged(int orientation) {
if (orientation==0){
Log.e("TEST", "orientation-Portrait = "+orientation);
} else if (orientation==90){
Log.e("TEST", "orientation-Landscape = "+orientation);
} else if(orientation==180){
Log.e("TEST", "orientation-Portrait-rev = "+orientation);
}else if (orientation==270){
Log.e("TEST", "orientation-Landscape-rev = "+orientation);
} else if (orientation==360){
Log.e("TEST", "orientation-Portrait= "+orientation);
}
}};
My question:
Is there a better solution to change activity-layout between "Landscape"
and "Landscape-reverse"
orientation?
Any suggestions are highly appreciated.
android:screenOrientation
attribute you can't use|
. --2) Configuration.orientation has nothing to do with ActivityInfo.SCREEN_ORIENTATION_PORTRAIT or the other constants. – Sarah