I am new to Orientation in Android. I have worked only for portrait mode so far, now I am working for both orientations.
I am using this method in manifest file for orientation :---
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden">
The Problem is :-
I have two layout Folders for this - one is layout & other is layout-land for both orientations.
Now I run my app sometimes it takes layout xml file on both orientation & some times layout xml file & layout-land xml file both works .
How to resolve this problem?
& If i use this code :--
#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);
}
}
Then orientation problem gets resolved but Activity is not created again, so my code works only one time . My Button is not working now.
2. My second question is that For Both XML File How to manage Code in Config Method
I want to know the complete concept of Orientation .
Answer will be highly Appreciated.
Thanks & Warm Regards,
Deepanker Chaudhary
layout is same as layout-land for portrait orientation it is layout-port folder. There is no need for you handle the layout file for orientation changes. All you need is to save the data on orientation change and restore it when displaying it. You can use the same file name for both the orientations. You can get some help from here for orientation handling. check, also this.
Please make sure below point :
android:configChanges
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
Related
I am developing an app that needs to run strictly in portrait mode in smart phones and strictly in landscape mode in tablets. I am using the following code to set the orientation manually in onCreate method of my login activity:
if (getResources().getBoolean(R.bool.portrait_only)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
Where the bool value comes false in Tablets. However the orientation is changed to landscape; still the layout file is picked from "layout_large" folder instead of "layout_large_land" folder. Hence my view looks stretched.
I have tried this link.
But it doesn't help.!
Any help is appreciated.
Thanks in Advance.
I had the same problem. Make sure you're NOT setting android:configChanges="orientation" for your activity in AndroidManifest.
I'm guessing the problem with that is that setRequestedOrientation potentially triggers an orientation config change, HOWEVER by using android:configChanges="orientation" this config change is not registered by the system.
Alternatively, if you want to keep the Manifest setting, you would have to implement your own recreation mechanic for this particular configuration change.
It may look something like this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(getResources().getBoolean(R.bool.portrait_only))
recreate();
}
I have used a folder like this for Tab & mobile
layout folder is for mobile and layout-sw720dp for Tabs. 720 dp is width which tells 720+ dp will use that folder for layout.
Being late in this thread but will help in the future SO. In my case I was programmatically changing the orientation of the screen by calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
OR
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
depending on the user's action. I have also created a separate layout for portrait and landscape but every time on changing the screen from portrait to landscape, it always loading the portrait screen in landscape.
In AndroidManifest.xml, I've set this
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
So, To solve this problem, Need to remove the orientation from android:configChanges then it works. This is what android documentation Link mentioned
The "orientation" value prevents restarts when the screen orientation
changes.
I have an activity with two layout files, one each for portrait and landscape modes. I see that the onCreate method is called each time I change orientation. I tried using
android:configChanges = "orientation"
in the manifest file, but it only reads the portrait layout. Is there a way I can set the layout to the landscape mode without calling onCreate?
If you use android:configChanges = "orientation", you must override the onConfigurationChanged(...) method of your Activity. Check the newConfig parameter for the new orientation then manually set the layout you want.
See the following links:
onConfigurationChanged(Configuration newConfig)
Handling the Configuration Change Yourself
You could create two layout folders. One for portrait mode and one for landscape.
layout and layout-land.
This is a fact of life with android. You need to develop your activity around the fact it can be destroyed at any minute due to a configuration change like the screen's orientation changing.
Save all the the information used to build the screen in on pause and recreate it in onCreate.
If you want your activity be only in landscape mode, you set this attribute for the activity in the AndroidManifest.xml:
android:screenOrientation="landscape"
You can put your layout.xml files in such folders for different screen orientations:
res\layout-land
res\layout-port
I want to detect layout change in my activity while keeping the widgets where they are. Is there any way to do this. I tried setOrientation(LinearLayout.HORIZONTAL) and it doesn't seem to work. Any ideas?
Try to add android:configChanges="orientation" in your manifest file in the activity and you may be able to get the orientation in the onResume method of your activity by using
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
//WHAT TO DO IN LANDSCAPE
} else {
//WHAT TO DO IN PORTRAIT
}
As a lower level solution, you could use one of the Sensors, to detect the orientation/tilt of the device. If you don't want layout changes you can constraint your activity to portrait or landscape only in the manifest...
You can play with the app Elixir to see what values a specific sensor returns.
I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.
My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.
The same error occurs if I start in landscape and try to move to portrait later on.
It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?
Also. I'm already using android:configChanges="orientation" in my manifest for the activities.
If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(...);
}
Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.
Make sure both xml files present in two different folders have same name.
I created layout and layout-land folders, in Manifest i have
android:configChanges="keyboardHidden|orientation"
If i remove the android:configChanges from the Manifest I can see the layout change from portrait to landscape...the problem is that the previous screens are destroyed when trying to go back.
I am assuming i could add some code in my Java to tell it when to change layouts. I have read a bunch of posts already but am confused where in the code it would happen.
I am trying to use some prepackaged code because I am new to Android. The link below is the Java code for the menu screen in particular i need to change layouts for. thanks so much!!
menu Java
Adding android:configChanges="keyboardHidden|orientation" will stop re-creation of your Activity and also prevent from changing the layout to layout-land/portrait layout. In that case you have to manage it manually from the java code. If you don't want your Activity to get re-created when you rotate your device/ change orientation. What you can do is just change the layout when your screen orientation changes inside onConfigurationChanged() method,
Check my answer here, how can that be done.
My work around for these problem was
in manifeast file you can use android:configChanges="keyboardHidden|orientation" if you wont these line then activity will be destroyed and created again.
and we have a below method
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
setContentView(R.layout.yourlayoutname);
}
here you can handle the orientation change...