Show different layout for both orientations - android

In my android application i designed two different layouts with same file name say my_profile.xml, and stored in two different directories i.e, 1) res/layout, 2) res/layout-land. Now the problem is if i start activity in Portrait mode it loads Portrait mode layout but after changing orientation it doesn't change layout, But if i start activity in Landscape mode it loads layout of landscape i.e, perfect. Problem is only when i change orientation, it doesn't handle it automatically. Can any one tell me, what can be the problem?

Check for the following :
1) In manifest file check for the below line in your activity
android:configChanges="orientation|keyboardHidden|screenSize"
2) Override the below function
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d(tag,"onconfig");
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// do something
Log.d(tag,"land");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
// do something
}
}
-Preeya

In your manifest.xml make the changes like this,,,,,
<activity android:name=".myActivity" android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden"></activity>
dont use configuartionChanges="orientation"
Thats it...

Check your manifest file. If you have the following in your activity then REMOVE IT:
android:configChanges="keyboard|orientation|screenSize"
orientation causes the the activity to resort to the same layout rather than creating a new one.

Related

Change screen orientation in Android without reloading the activity

I want to change the orientation programmatically while running my Android App, with these lines of code:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
They work so far, but the main problem is that the whole activity is reloaded when the screen orientation changes, and I don't want that. Is it possible? Thanks.
EDIT: OK, after I while I found out what I was missing. I had to include also "screenSize" in the configChanges property, so having
android:configChanges="orientation|screenSize"
solved the whole thing.
See the edit by #luisfer. For targeting Android 3.2 and above, you need BOTH
android:configChanges="orientation|screenSize"
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter
In AndroidManifest file add android:configChanges="orientation" for the activity you want to handle this orientation
In activity use onConfigurationChange overrided method. Do task you want to handle in orientation change.
Ansewered here:
Android, how to not destroy the activity when I rotate the device?
Add:
android:configChanges="orientation"
To your androidmanifest.
see:
http://developer.android.com/guide/topics/manifest/activity-element.html#config
Call this method And Set manifest file
android:configChanges="orientation|screenSize|keyboardHidden"
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();
}
}

Tab Widget Issue when use android:configChanges="orientation|keyboardHidden" in Grid View but working for other Tab

I am stuck with my issue.Thing is that its a custom Tab Widget.In that have multiple
tab like Home - News - Abc - PQR .
The Activity should be for both orientation like portrait and landscape. so for that each tab have two xml for portrait which is store at layout-port/file.xml and landscape which store at layout-land/file.xml
For manage orientation portrait to landscape i have added android:configChanges="orientation|keyboardHidden" rule tag in each activity.
TAB_SAMPLE.java Tab file.
TAB_GROUP_ACTIVITY each Tab Group activity File
file.java Task file
After all this stuff i get issue here :
If i addandroid:configChanges="orientation|keyboardHidden"rule tag in tab_sample activity then its working perfect. like manage different view. port to land and land to port but its not working in Home.java.
Now if i remove android:configChanges="orientation|keyboardHidden" rule tag in tab_sample activity then its working for Home activity not for News.java
Mean when i change the orientation its keeping same xml form port not use from layout-land.in the sense its call OnCreate() again.
So as i found may be issue is in Tab Widget.
Update
Now after tracing my code i get that main issue is in grid view activity because its only activity which is not working.
Issue is between Tab host v/s Grid View. I don't know why its not taking layout-land xml file. i found this as same issue but no replay on that question also
see in Detail manifestfile.xml
I want to maintain both portrait and landscape in all activity.
Both XML File
Please help me how to solve this.
Oooohhh Finally i got the solution for above issue. It's was very difficult.
For maintain the state of orientation Landscape to portrait and vice-versa we generally adding android:configChanges="keyboardHidden|orientation" property tag under activity.
But here may be issue is Tab_Group_ Activity due to that i am not able to maintain state in GridView. Grid_File.java is Only single java file which was not handling the orientation rest of all other working perfectly.
Now if i remove android:configChanges="keyboardHidden|orientation" from TAB_SAMPLE.java then Its handling only Grid_File.java not others.
mean that was keeping same Layout XML in landscape also where i have two separate XML File.
Here is my solution:
I have add android:configChanges="keyboardHidden|orientation" in TAB_SAMPLE.java as well as
implement onConfigurationChanged(Configuration newConfig) and set Number of column of grid. like gridView.setNumColumns(6);
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// gridView.setSelection(index);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// Log.e("On Config Change", "LANDSCAPE");
gridView.setNumColumns(6);
} else
{
// Log.e("On Config Change", "PORTRAIT");
gridView.setNumColumns(4);
}
}
Generally we are adding either android:configChanges="keyboardHidden|orientation" tag under activity or implementing onConfigurationChanged(Configuration newConfig) but here i have written both.

Orientation change and handle ConfingurationChanges manually in android

Here i develop one android application for both orientation Portrait and Landscape. So i create two layout folder for Landscape and Portrait and create two layout.xml files one for Portrait and second one is for landscape. In portrait layout.xml file has one ImageView and In Landscape layout.xml file has two ImageView. In AndroidManifest.xml, i set parameter android:configChanges="keyboard|orientation" in <activity> entry to stop recreating activity on orientation changes. and In my Activity i override onConfigurationChanges() as below
public void onConfigurationChanged(Configuration newConfig)
{
try
{
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Toast.makeText(myContext, " Config Changed ", 1000).show();
setContentView(R.layout.double_pageread_layout);
setLayout(); // Initialize Controls (Buttons, ImageView,etc..)
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
But the Problem is that when i run my application. i can see it is perfect But When i change orientation it does not display anything.
Please Help me.
Thanks In Advance.
You do not need to manually change the layout by handling configuration changes. Simply place the two different layout.xml files into the res/layout and res/layout-port folders and Android will automatically inflate the proper one on rotate.

How to handle orientation change easily

I'm working on a android application that adds every view programmaticaly.
When the user turns the screen, I just want to values that are filled in to be displayed again.
Is there an easy way to let Android do this automaticaly?
My application is completely dynamic so it doens't have a predetermined layout, which makes it a lot harder.
So, how can I easily save the state of my screen?
Everytime orientation change, android create new view and destroy the old one. You can saved your data when orientation change and re-initialize when the new view is created
Use onConfigurationChanged method of activity to detect Orientation Change
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();
}
}
Don't forget to edit the appropriate element in your AndroidManifest.XML like this to include the android:configChanges
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
For an expanded explanation of Hein's correct answer see my previous post:
https://stackoverflow.com/a/57239493/5696328

how to Manage Child activity within Tab Acivity when orientation changes?

I'm using tab widget.
When my child activity is running and I change the orientation,child activity destroy.
As a solution of this problem,I added
android:configChanges="orientation|keyboardHidden in all activity tags in my manifest.xml file.
I found that my app doesn't take xml file from layout_land folder.
Can anyone give me solution for this query?
Thanks in advance.
I found that using onConfigurationChanged method i can know the orientation and i have to set landscape file in layout folder instead of layout_land folder.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.login_landscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.login);
}
}
This happens because your Activity is not destroyed since you have put android:configChanges="orientation|keyboardHidden" in manifest so the setContentView is not called when orientation changes and therefore it doesn't pick the layout from the layout_land
Update : And this is not a problem with your Tab-Activity try it in normal activity it will not pick the correct layout or in general speaking no layout is picked your portrait layout is only rotated to be displayed in landscape

Categories

Resources