Change User interface when device change the orientation android -
i have screen, show different user interface when device lanscape , portrait. shows listview when device portrait, , shows gridview when device lanscape. looks youtube app or google play store app.
thanks reading , answering question.
edit: want add more details question:
- i using fragment manage view.
- i need save data , in view when rotate (avoid reloading lot of data).
- i tried add android:configchanges on manifest , use onconfigurationchanged in fragment. not success.
i hope see particular example or detail solution. answer appropriated. thanks
there 2 things develop functionnality.
first have make 2 different layouts:
res |- layout |- mylayout.xml |- layout-land.xml |- mylayout.xml in layout/mylayout.xml file, include listview need , in layout-land/mylayout.xml, include gridview.
android select automatically correct layout, depending of current screen orientation, when 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 null in landscape // mgridview null in portrait // !!!!!!! } setcontentview(r.layout.mylayout); now, in code need check in orientation make if/else statements.
to detect in orientation are, follow following states:
create file in
res/values/bool.xml<resources> <bool name="islandscape">false</bool> </resources>create file in
res/values-land/bool.xml<resources> <bool name="islandscape">true</bool> </resources>now in activity can test, every time need, if in landscape or portrait mode , apply corresponding action
boolean landscape = getresources().getboolean(r.bool.islandscape); if (landscape) { // gridview } else { // listview }
to respond edit:
fragments similar activities this.
if want save data during orientation change, follow official documentation on topic, part on retaining object during configuration change.
you should not use android:configchangesin manifest explained in documentation:
handling configuration change can make more difficult use alternative resources, because system not automatically apply them you. technique should considered last resort when must avoid restarts due configuration change , not recommended applications.
Comments
Post a Comment