Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change User interface when device change the orientation android

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

like image 449
Thanh Le Avatar asked Mar 12 '26 00:03

Thanh Le


2 Answers

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.

like image 62
ol_v_er Avatar answered Mar 15 '26 00:03

ol_v_er


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.

like image 32
Oam Avatar answered Mar 14 '26 23:03

Oam