3

I have a screen, that show different user interface when device is lanscape and portrait. It shows a ListView when device is portrait, and it shows a GridView when device is lanscape. It looks like youtube app or google play store app.

Thanks for reading and answering my question.

EDIT: I want to add more details about my question:

  • I am using fragment to manage this view.
  • I need save all data and in this view when it rotate (avoid reloading a lot of data).
  • I tried to add android:configChanges on manifest and use onConfigurationChanged in this fragment. But it was not success.

I hope see the particular example or a detail solution. But any answer is appropriated. Thanks

Thanh Le
  • 1,370
  • 2
  • 19
  • 30

3 Answers3

7

There is two things to develop this functionnality.

First you will have to make two different layouts:

res
|- layout
   |- mylayout.xml
|- layout-land.xml
   |- mylayout.xml

In the layout/mylayout.xml file, include a ListView where you need it and in layout-land/mylayout.xml, include your GridView.

Android will select automatically the correct layout, depending of the current screen orientation, when you call setContentView.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);
    mListView = (ListView) findViewById(R.id.mylistview);
    mGridView = (GridView) findViewById(R.id.mygridview);
    // !!!!!!!
    // mListView will be null in landscape
    // mGridView will be null in portrait
    // !!!!!!!
}

setContentView(R.layout.mylayout);

Now, in code you will need to check in which orientation you are to make if/else statements.

To detect in which orientation you are, follow the following states:

  1. create a file in res/values/bool.xml

    <resources>
        <bool name="isLandscape">false</bool>
    </resources>
    
  2. create a file in res/values-land/bool.xml

    <resources>
       <bool name="isLandscape">true</bool>
    </resources>
    
  3. Now in your activity you can easily test, every time you need, if you are in landscape or portrait mode and apply the corresponding action

    boolean landscape = getResources().getBoolean(R.bool.isLandscape);
    if (landscape) {
        // do something with your GridView
    } else {
        // do something with your ListView
    }
    

To respond to your edit:

Fragments are very similar to Activities for this.

If you want to save data during orientation change, follow the official documentation on this topic, especially the part on Retaining an Object During a Configuration Change.

You should not use the android:configChangesin Manifest as explained in the documentation:

Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

ol_v_er
  • 27,094
  • 6
  • 48
  • 61
  • Thanks for your answer. As your solution, if the screen is rotated, where i should put the data? in the application object or somewhere else? Thanks – Thanh Le May 15 '13 at 14:14
  • If you want to save data during orientation change, follow the official android explanation: http://developer.android.com/guide/topics/resources/runtime-changes.html – ol_v_er May 15 '13 at 14:16
  • Thanks. I am using fragment, and it doesn't have onRetainNonConfigurationInstance(). I have to implement this function in the parent activity, dont I? But I want to do everything inside fragment. Do you have any solution for this? – Thanh Le May 15 '13 at 14:25
  • No there is not. There is a Fragment method, [setRetainInstance](http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29) which should to the trick. – ol_v_er May 15 '13 at 14:34
  • Thansk you so much. I think I can slove this problem. But can you explain more detail about setRetainInstance for me. I wil mark your answer as accepted answer. Thanks alot. – Thanh Le May 15 '13 at 14:47
  • There should be no impact on the Layout management. The `onCreateView` method should still be called. Is it? – ol_v_er May 15 '13 at 15:37
  • Thank you very much. You made my day. I have solve this problem. Thanks againg. – Thanh Le May 15 '13 at 16:26
1

Try this and changing your layout accordingly.

public String getOrientation(Context context){
    final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
           switch (rotation) {
            case Surface.ROTATION_0:
                return "portrait";
            case Surface.ROTATION_90:
                return "landscape";
            case Surface.ROTATION_180:
                return "reverse portrait";
            default:
                return "reverse landscape";
            }
        }

This method detects and returns the device orientation change. Might be quite useful.

Oam
  • 912
  • 6
  • 7
1

A good way of saving data in an application which is independent of the activities in the application is to have an application object. Although the documentation says "There is normally no need to subclass Application", I find them very useful. Your application class needs to be mentioned in the application manifest, and it gets created before any activity objects. So store all your data in your application class, and then it won't matter what happens to your activities when the orientation changes.

Stochastically
  • 7,616
  • 5
  • 30
  • 58