Android Landscape/Portrait changing difficults - android

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

Related

How to change layout when rotate?

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
}
}

keep activity and use different layout (land or portrait) when rotate screen

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.

How to fix screen orientation on android for different devices

I want to disable changing orientation in my android app, but I want to allow user use his native orientation: landscape for tablets and portrait for phones.
I try to set "android:screenOrientation" with different values, but I can't find appropriate value.
Is such task needs to write java code (and what code I need to write), or this can be solved using manifest?
UPD As in #Pierrew's hint, I need to create res/layout/main.xml for default layout, res/layout-land/main.xml for landscape layout and res/layout-port/main.xml for portrait layout. So, I need to know, how to defined all layout in res/layout/main.xml and only clarify orientation in res/layout-land/main.xml and res/layout-port/main.xml.
I think I need to declare layout in res/layout/main.xml, and derive all layout properties in res/layout-land/main.xml and res/layout-port/main.xml and override only android:screenOrientation value in derived layout.
How I can do it?
setting android:screenOrientation="landscape" in the layout xml is the way to do it. To support different devices (tablet and phones), you need to use Size-Qualifiers. In your res folder, you will have to create a res/layout-large/main.xml to support tablet. So if you want landscape for tablets. In your res/layout-large/main.xml, you put android:screenOrientation="landscape". In your normal res/layout/main.xml, you can put android:screenOrientation="portrait". Android will automatically detect what device the user is using and load the appropriate xml. For further information, check out the size qualifier section in http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSizeQuali
You can use a little logic using these methods.
Boolean isTablet = (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
Change Screen Orientation programatically.
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActivityInfo:
http://developer.android.com/reference/android/content/pm/ActivityInfo.html
This should do the trick for you:
import android.provider.Settings;
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled){
Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
Add permission to the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Android Screen orientation landscape error?

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"

Android newbie: switch between landscape mode and portrait mode

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.

Categories

Resources