Changing the orientation of a fragment is a better test than for landscape

In Landscape mode, I have two FrameLayouts controlled by One Activity, and the use of two fragments. In the "Portrait" mode, I have one FrameLayout controlled by one action; when I select a line, I call another action to display the details using a fragment

In the Portrait operation, I check the orientation in the onCreate () method.

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

It works well, but

The problem occurs when I have a "small" device. In this case, in the "Landscape" mode, I do not need a view with two fragments, but I want to behave like a "portrait" view. However, since the device is actually in "Landscape", when detailed actions are started automatically, it ends.

So questions are the best way to handle this?

+1
source share
2 answers

or create your own resource using the bool value (from google io 2012)

<!-- in your values/custom.xml --> <resources> <bool name="small_screen">true</bool> <bool name="normal_screen">false</bool> </resources> <!-- in your values-sw320dp/custom.xml --> <resources> <bool name="small_screen">false</bool> <bool name="normal_screen">true</bool> </resources> 

NOTE. You must define the minimum screen width (sw320dp) for which you think the screen is not small ( link with additional information )

The advantage is that you can read this value at runtime, and you can have special cases for special resource qualifiers ... for example. you can read this value at runtime by invoking your activity:

 if(getResources().getBoolean(R.bool.small_screen)) { // You have a screen which is < 320dp } else { // You have a screen which is >= 320dp } 

You can even use this logical resource in your manifest to start a completely different action for small screens

 <activity android:name="SmallScreenActivity" android:enabled="@bool/small_screen"> <!-- ENABLE FOR SMALL SCREEN --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="NormalActivity" android:enabled="@bool/normal_screen"> <!-- ENABLE FOR OTHER --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

This way you can just use Activity for the normal case (android: enabled = "@ bool / normal_screen") and use special activity for the small screen android: enabled = "@ bool / small_screen"

A WARNING. This method will not work on newer devices since cellular. You can read why this method is no longer allowed or read about working with a similar solution.

+1
source

Complete an additional screen size check before checking orientation. Given that the small device has a width of 500 pixels and a height of 600 pixels, you would do something like this.

 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); i int width = size.x; int height = size.y; if ( width > 500 && height > 600 && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; } 
0
source

All Articles