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 hope see the particular example or a detail solution. But any answer is appropriated. Thanks
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:
create a file in res/values/bool.xml
<resources>
<bool name="isLandscape">false</bool>
</resources>
create a file in res/values-land/bool.xml
<resources>
<bool name="isLandscape">true</bool>
</resources>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With