Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different layout for "Landscape" and "Landscape-reverse" orientation

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.

like image 525
Rami Avatar asked Jan 11 '16 19:01

Rami


People also ask

What is Landscape reverse?

Reverse-landscape is defined to be a rotation of the print-stream page to be imaged by -90 degrees with respect to the medium (i.e. clockwise) from the portrait orientation. Note: The 'reverse-landscape' value was added because some applications rotate landscape -90 degrees from portrait, rather than +90 degrees.

Can we create different layout files for portrait and landscape mode?

Android App Development for Beginners Step 3 – Create a layout file by right-clicking on the resources, name the file, from the 'Available qualifiers, select Orientation. Click >> option. Select Landscape from UI mode.

What orientation is landscape?

What Does Landscape Mean? Landscape is a horizontal orientation mode used to display wide-screen content, such as a Web page, image, document or text. Landscape mode accommodates content that would otherwise be lost when viewed to the left or right.


1 Answers

Are you trying as suggested here?. You may handle an event with a different types of configuration reverse and standart by using activity attribute sensorLandscape

EDITED: Try to use Display.getOrientation as described here http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html

And do not forget to set configChanges flag on activity in manifest to handle changes manualy in onConfigurationChanges().

So it seems like only way to do this is to listen SensorManager as frequently as possible.

SensorManager sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMan.registerListener(...)
like image 91
N. Chebotarev Avatar answered Oct 02 '22 16:10

N. Chebotarev