I followed the android developrs tutorial to create a simple tab layout. Everything is fine with the tutorial, I got the tab layout working.
In default portrait mode, the tabs are located on top of screen, but when change to landscape mode, the tabs on the top makes the screen looks odd, so, I would like to locate the tabs on the left side of the screen vertically when the emulator change to landscape mode.
I am not sure What is the correct way to do it? I mean the correct way to define different tabs layout for portrait mode and landscape mode. Anybody can give some suggestions?
Create two layout files one for portrait and one for landscape. place these layout-port and layout-land respectively. You'll have to handle screen orientation if u want to save the state of the screen
You need to set up 2 different layouts for the activity, one in portrait ("normal") mode, one in landscape mode. This implies to not use TabActivity.
Put landscape_tab.xml in layout-land folder inside res folder (res/layout-land)
Put portrait_tab.xml in layout-port folder inside res folder (res/layout-port)
OfCourse you have to create layout-land and layout-port folder in res directory manually
Your Solution is here
android:orientation="vertical" does not work for TabWidget
cheers :)
On your AndroidMainfest.xml add
<activity android:name=".MyActivity"
android:configChanges="orientation" >
</activity>
And when application changes orientation, override the method on your MyActivity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// do what you want
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
// do what the other thing you want
}
}
This from: http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
Tip: When orientation changes, the Activity is restarted if you don't add android:configChanges.
Related
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.
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
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.
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
}
}
I want keep activity and use different layout (landscape or portrait) when rotate screen.
layout (portrait) : there 3 textview, one is visible and other textview is gone
layout-land : there 3 textview and all is visible
Followed best answer from here >> Activity restart on rotation Android , I put
android:configChanges="keyboardHidden|orientation|screenSize"
on my manifest and i put this code :
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}
efect
before put above code : Activity and data is keeped but cannot change screen layout land/portrait
after put above code : Activity is keeped, layout is changed, but data is lost
so how to solve it ?
When you write this:
android:configChanges="keyboardHidden|orientation|screenSize"
You are telling the system you will handle the rotation change yourself and so it doesnt do automatic stuff it usually do like saving the data through viewing mode change.
There is no need for this line, just create another layout directory with a resource qualifier "land", the dir should be named "layout-land" and put in it the layout you want the app to have in landscape mode and the data will be saved through the change.
Like so:
Ignore the contents of the directories in the image.
I'm new to android and making an app in which there is layout design for both portrait and landscape mode. The app is running fine in both screen orientation except for one activity. The activity is working fine in portrait mode when i go from one activity to another, but crashes in landscape mode. I tried to solve this in different ways through Google search but didn't succeed. Please someone help me. Thanks
Use this android:configChanges="orientation" inside your activity tag in AndroidManifest.xml hope this will help
Use this in Manifest.
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="VncCanvasActivity">
This line specifies the screenOrientation as landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden"
Make two different resource folders in res folder like below:
1) layout --> put your main.xml
2) layout-land --> put your same main.xml in here too.
Note: in both res folders the name of layouts must be same.
Edit: well android handles orientation change automatically after above mentioned
procedure.. but if you want to handle it manually then here is the code to handle..
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
Do something in Portrait
}
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Do something in Landscape
}
}
Add this below line in your manifest to the activity you want to handle orientation:
android:configChanges="orientation|screenSize|keyboardHidden"
Target:
If I'm switching ffrom a Portrait to a Landscape the complete layout should change (same buttons, same textfields, but different positions). Until now the problem is that they lose the values.
I've set
<application ...
...android:configChanges="orientation|keyboardHidden" ...>
2 different Layouts in:
+Layout
+-main.xml
+-second.xml
+Layout-land
+-second.xml
ex: I switch from a portrait to a landscape, I want to save all the portrait values (buttons, textfields) and restore them in the landscape view.
Now I'm missing some input for the method in MyActivity()
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// how should i handle this?
}
I've found hints like setContentView(...) and "handle this Problem in the Manifest file". Maybe my approach to this is completely false. I would appreciate any hints!
Just let android handle the orientation changes?
You got the xml-files right: Two different layouts with the same name in the two folders layout/ and layout-land/
Just don't set
<application ...
...android:configChanges="orientation|keyboardHidden" ...>
and Android will handle everything for you :)
Cheers
Ali3n
I simply want to do:
if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
setContentView(R.layout.search_portrait);
else
setContentView(R.layout.search_landscape);
The problem is - getRequestedOrientation always returns -1.
Any suggestions?
If you're just changing the layout, you should really think about putting your portrait layouts in /res/layout and your landscape layouts in /res/layout-land (with the same names) and Android will select the correct layout automatically.
Try adding android:screenOrientation="portrait" or android:screenOrientation="landscape" to your manifest for this activity. It will be the default orientation... that's because getRequestedOrientation "Return the current requested orientation of the activity. This will either be the orientation requested in its component's manifest, or the last requested orientation given to setRequestedOrientation(int).".
It must works properly, use this
if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main_activity);
And also place landscape xml file in
/res/layout-land
Note: Layout file name must be same in all layout directories
i.e
res/layout/activity_main
res/layout-land/activity_main