Specify different layouts for portrait and landscape orientation?

I specified two separate layout XML files for activity, one for the Portrait folder in the res / layout folder and one for Landscape in the res / layout-land folder. my actual problem is that when I run the application in portrait mode, it takes ui from the layout, and I instantly rotate my phone, it does not accept xml from the layout-earth folder, it takes ui from the layout folder, and if I run his application in landscape mode, takes xml from the layout-ground folder and instantly rotates my portrait mode, it takes xml from layout-land ui does not change to portrait ui.

can any buddy tell me what the problem is, are there any settings required for this plz help me solve this problem.

Thanks in advance.

this is my landscape ui xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget57" android:layout_width="400dp" android:layout_height="400dp" android:layout_gravity="center|center_vertical" android:layout_marginTop="30dp" android:orientation="vertical" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" > <TextView android:id="@+id/txtName" android:layout_width="90dp" android:text="@string/UserName" /> <EditText android:id="@+id/edName" android:imeOptions="actionNext|actionDone" android:paddingRight="10dp" android:singleLine="true" android:width="240dp" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/txtPass" android:layout_width="90dp" android:text="@string/PassWord" /> <EditText android:id="@+id/edPass" android:imeOptions="actionGo" android:paddingRight="10dp" android:password="true" android:singleLine="true" android:width="240dp" /> </TableRow> <LinearLayout android:id="@+id/widget57" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:orientation="vertical" > <Button android:id="@+id/btnLogin" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="@string/btn_GO" android:textSize="19sp" > </Button> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:text="CoinOp Solutions" android:textSize="30sp" android:textStyle="bold" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="Version 0.1" /> </LinearLayout> </TableLayout> this is my Portrait ui <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget57" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/txt_Required" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="" android:textColor="#ffff0000" > </TextView> <TextView android:id="@+id/txtName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="@string/UserName" > </TextView> <EditText android:id="@+id/edName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:imeOptions="actionNext|actionDone" android:singleLine="true" android:textSize="18sp" > </EditText> <TextView android:id="@+id/txtPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/PassWord" > </TextView> <EditText android:id="@+id/edPass" android:layout_width="fill_parent" android:layout_height="wrap_content" android:imeOptions="actionGo" android:maxLines="1" android:password="true" android:singleLine="true" android:textSize="18sp" > </EditText> <LinearLayout android:id="@+id/widget57" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:orientation="vertical" > <Button android:id="@+id/btnLogin" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="@string/btn_GO" android:textSize="19sp" > </Button> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:text="CoinOp Solutions" android:textSize="30sp" android:textStyle="bold" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="Version 0.1" /> </LinearLayout> </LinearLayout> 

My manifest file

  <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:debuggable="true" android:icon="@drawable/coinop" android:label="@string/app_name" android:theme="@style/CustomTheme" > <activity android:name=".LoginActivity" android:configChanges="orientation" android:label="@string/app_name" android:windowSoftInputMode="stateVisible|adjustPan" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ShowDbList" android:configChanges="orientation" android:label="@string/app_name" android:windowSoftInputMode="adjustPan" > </activity> <activity android:name=".Menu" android:configChanges="orientation" android:label="@string/app_name" android:windowSoftInputMode="adjustPan" > </activity> 
+4
source share
3 answers

You have android:configChanges="orientation" in your manifest. According to the doc, configChanges contains a list of actions that you will handle manually yourself.

If you remove android:configChanges="orientation" , the layout will be changed when the phone is rotated. Otherwise, you must deal with it yourself.

+7
source

Not sure if this helps, but you can try (if you haven’t already).

  • in an operation that uses the layout, it overrides onConfigurationChanged and setContentView again.
  • remove android: configChanges = "orientation" for the operation that uses the layout
+2
source

you should try to add:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); } 

} And set the layout manually through setContentView (..) in Activity. Hope this helps. Check again: http://developer.android.com/guide/topics/resources/runtime-changes.html

+1
source

Source: https://habr.com/ru/post/1402852/


All Articles