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.
Related
I do not know if the title is correct, here is what happens.
I have an application which works differently on a phone and on a tablet, on a phone it shows as portrait on a tablet it shows as landscape.
To achieve this I created a class called CoreActivity which is extended by all my activities and does the following:
public class CoreActivity extends Activity {
protected boolean _landscape = false;
public boolean isPhone() {
int layoutSize = getScreenLayoutSize();
return (layoutSize == Configuration.SCREENLAYOUT_SIZE_SMALL || layoutSize == Configuration.SCREENLAYOUT_SIZE_NORMAL);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isPhone() && !_landscape) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
protected int getScreenLayoutSize() {
return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
}
}
My problem occurs when I wish to show a screen on a phone setup on landscape mode, to do this I use the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
_landscape = true
super.onCreate(savedInstanceState);
}
The problem is, that on a phone, if the user is holding the phone on portrait mode (as one would, since most of the application is in portrait mode) then the activity gets created and destroyed and then recreated. But if they are holding it on landscape mode, then it is only created once.
My problem occurs because on the onCreate method I launch the threads that load data, and I also show fragments.
Is there a way to avoid this problem? is there a way to launch an activity from the start on portrait mode and not change it, or have it not create twice?
Thanks in advance for any help you can provide
Recreating occurs just because you are forcing to, by calling setRequestedOrientation probably in order to set screen orientation. However you don't need to check and change it by code. You can do it via xml file. You can set different xml files depending on the screen size. But it is little bit hack so there is no guarantee in the future.
On the manifest file you can force by:
<activity
android:name="com.my.example.MyActivity"
android:screenOrientation="landscape"/> // or portrait
So as far as I understand you want to force portrait for small sizes and landscape(or sensor) for larger screen sizes. But above configuration applies for all screen sizes. Here is the tricky part: landscape portrait sensor etc. are all integers defined here.
As you might guess you can write android:screenOrientation=0 instead of landscape. What we know from beginning lessons of android, we can define integers in xml files then their values might vary on screen size. So..
You should first create different integers.xml files for different screen sizes. i.e.:
values
- integers.xml
values-large
- integers.xml
for values/integers.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="orientation">1</integer> // 1 for portrait
</resources>
for values-large/integers.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="orientation">0</integer> // 0 for landscape
</resources>
and finally you you have to manipulate manifest file by:
<activity
android:name="com.my.example.MyActivity"
android:screenOrientation="#integer/orientation"/> // read constant from xml files
You can also use sensor , fullSensor, nosensor, locked, behind, reverseLandscape, reversePortait options too. But I am warning you, this is a hack solution.
Well, I found a solution to this issue, just declare:
android:screenOrientation="locked"
on every activity having this issue in the manifest.
And keep using setRequestedOrientation() programatically to define if landscape or portrait orientation within onCreate() method,
It will work! ;)
If all your activites must have the same orientation on a device, you can start your application with a splash screen activity, set the orientation like in your current code and forward to your CoreActivity or any other activity in your App.
In your AndroidManifest.xml set
<activity
android:name=".CoreActivity"
android:screenOrientation="behind"/>
This will use the same orientation as the activity that's immediately beneath it in the activity stack.
Did you try adding android:configChanges="keyboard|keyboardHidden|orientation" to the <activity>-Tag inside of your AndroidManifest.xml?
This should prevent the system from restarting the Activity, but I am not sure whether this will actually work when forcing the orientation programatically. It's worth a shot though.
try this in your manifest.xml...
this will stop the recalling the onCreate() multiple time while orientation changes...
android:configChanges="keyboardHidden|orientation|screenSize"
You dont need to handle this manually. Android has built it support for different screen sizes
check this link
http://developer.android.com/training/multiscreen/screensizes.html
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.
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.
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
I want to make possible to use an activity in landscape / portrait. The activity starts with the layout for the current orientation (that means before starting the activity). After that it has to stick to it, and not react to orientation change.
I tried putting
android:configChanges="orientation"
in the manifest of the activity, and overriding
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
to do nothing (tried commenting the super call too, but this lead to exception)
but this has not the effect disabling the rotation change - the change is processed and the layout reconstructed, it just doesn't use the correct one.
And I can't use
android:screenOrientation
Because it seems I have to specify only one mode for always, and that's not what I need either.
And anyways, if I specify something there, the activity gets reconstructed when rotating.
Tried with
android:screenOrientation="nosensor"
that doesn't do anything
Here there's a lock of current orientation with code
http://www.samcoles.co.uk/mobile/android-lock-and-unlock-screen-orientation/
But it achieves the same effect as specifying orientation in XML (keeps layout but reconstructs the activity). It's a bit nearer to what I want (keeps orientation from start), but reconstructs the activity and I don't want it to react at all.
android:configChanges="orientation" doesn't work on the emulator at all, but it works fine on devices.
Try
#Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.orientation = getResources().getConfiguration().orientation;
super.onConfigurationChanged(newConfig);
}
Add this to onCreate
if (this.getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
else {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}