android - How to change layout when rotate? -


i have xml in layout-normal, layout-large , layout-land. i'm trying use provided xml in specific orientation.

i search here , i've tried.

1) used different layout name same ids in , override onconfigurationchanged , set layout there. here's code

@override public void onconfigurationchanged(configuration newconfig) {     super.onconfigurationchanged(newconfig);      if (newconfig.orientation == configuration.orientation_portrait)     {         setcontentview(r.layout.activity_login2);     }     if (newconfig.orientation == configuration.orientation_landscape)     {         setcontentview(r.layout.activity_login );     } } 

it changed when screen rotates inputed data in edittext gone. tried use onsaveinstancestate save instance still same. looks destroys activity , create new 1 widget initialized in oncreate gone.

2) found layout-land , put landscape layout there same layout name in layout-normal , layout-large example

res/layout-land -> activity_login.xml

res/layout-normal-> activity_login.xml

res/layout-large-> activity_login.xml

and removes onconfigurationchanged on code still doesn't work.

in androidmanifest in loginactivity put

     <activity         android:name=".loginactivity"         android:configchanges="keyboardhidden|orientation|screensize"         android:theme="@style/defaulttheme"         android:windowsoftinputmode="adjustresize">         <intent-filter>           <action android:name="android.intent.action.main" />           <category android:name="android.intent.category.launcher"/>         </intent-filter>     </activity>  

is there anyway change layout when screen rotated? thank in advance.

the "screen size" qualifier has higher precedence "orientation" qualifier. https://developer.android.com/guide/topics/resources/providing-resources.html#alternativeresources

android supports several configuration qualifiers , can add multiple qualifiers 1 directory name, separating each qualifier dash. table 2 lists valid configuration qualifiers, in order of precedence

suppose have these files:

res/     layout-normal/         layout.xml     layout-land/         layout.xml 

if have normal screen size device, won't matter whether use portrait or landscape. layout-normal chosen on layout-land.

you can solve 2 different ways.

first, put "default" layout in plain layout directory (instead of layout-normal). files be

res/     layout/         layout.xml     layout-land/         layout.xml 

second, combine qualifiers in order make obvious you're differentiating between portrait , landscape. files be

res/     layout-normal/         layout.xml     layout-normal-land/         layout.xml 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -