Reputation: 3257
I have an xml
in layout-normal
, layout-large
and layout-land
. What I'm trying to do is to use the provided xml
in specific orientation
.
I already search for it here and this are what I've already tried.
1) I used different layout
name but same ids
in it and override onConfigurationChanged
and set the layout
there. Here's my 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 does changed but when the screen rotates the inputed data in the EditText
is gone. I tried to use onSaveInstanceState
to save the instance
but still the same. It looks like it destroys the activity and create a new one where all my widget that is initialized in onCreate
is gone.
2) Then I found layout-land
and just put the landscape layout there with same layout name like in layout-normal
and layout-large
example is
res/layout-land -> activity_login.xml
res/layout-normal-> activity_login.xml
res/layout-large-> activity_login.xml
and removes the onConfigurationChanged
on the code but still doesn't work.
In my AndroidManifest
in LoginActivity
I 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 to change the layout when screen is rotated? Thank you in advance.
Upvotes: 1
Views: 10018
Reputation: 54204
The "screen size" qualifier has higher precedence than the "orientation" qualifier. https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
Android supports several configuration qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2 lists the valid configuration qualifiers, in order of precedence
Suppose you have these files:
res/
layout-normal/
layout.xml
layout-land/
layout.xml
If you have a normal screen size device, it won't matter whether you use portrait or landscape. layout-normal
will always be chosen over layout-land
.
You can solve this two different ways.
First, you could put your "default" layout in the plain layout
directory (instead of layout-normal
). So your files would be
res/
layout/
layout.xml
layout-land/
layout.xml
Second, you could combine qualifiers in order to make it obvious that you're differentiating between portrait and landscape. So your files would be
res/
layout-normal/
layout.xml
layout-normal-land/
layout.xml
Upvotes: 5
Reputation: 34190
First of all you don't require to create another .xml
like activity_login2
, you can use same activity_login.xml
.
Android have default folder /res/layout which includes all your layout. So whenever you rotate device it will use same activity_login.xml file.
you can add a new folder /res/layout-land, copy activity_login.xml into it and make the needed adjustments.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.activity_login2); // it will use .xml from /res/layout
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.activity_login); // it will use xml from /res/layout-land
}
}
Upvotes: 1
Reputation: 521093
I think you might be missing the point of the layout-land
folder. It should be the case that whatever layout you specify there, say some_activity.xml
, will be automatically used when the device rotates to landscape. You can name the layout file the same exact name which is already being used by your potrait version. And the layout file itself can also use the same IDs to name the various widgets which appear.
With regard to you losing some UI state when the device is rotated, you might have to override onSaveInstanceState(Bundle savedInstanceState)
and save some state from your UI when a rotation happens. This doesn't really have anything to do with which layout file gets shown though.
Upvotes: 2