orientation change is not working when I rotate a child activity to landscape. I added android:configChanges="orientation|keyboardHidden in all activity tags in my manifest.xml file.
I found that my app doesn't take layoyt file from layout_land folder. what is the solution for this problem.
By putting that in your manifest you are saying that you will manually handle all orientation changes. You'll have to setContentView again onConfigurationChanged OR remove it from your manifest and it will load the right layout.
Edit: found this answer afterwards which explains it
Related
I'm checking the size of the device and if it's not tablet I'll set orientation to portrait so I can't set orientation at Manifest. I'm checking the size and orientation at each Activity like this:
if(Services.isMobile()){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
But it has two problems 1.I have to do like so an all activities (and there is a lot of them)
2.It doesn't work for Fragments.
You should add android:screenOrientation attribute to <activity> declaration in your manifest. For more details on what values the attribute accepts, see documentation
You must declare it in the manifest file of every activity like
android:screenOrientation="portrait"
Or you can create a base class and make it abstract
StackOverflow portrait mode only
You can perform that programmatically :
You should test in all activities, or create a class and implement it in every activities.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Use it in FragmentActivity & it will work on fragments. Just be sure to have a correct layout
I did some researches about this question but I couldn't find my answer. So, yes I did create a folder under res called "layout-land" and I put a separate main.xml in it. I intentionally deleted some images in the main.xml in the layout-land and I noticed that these images were still there when I rotated the android phone to horizontal.
Many questions similar to mine are caused by adding onConfigurationChanged to manifest. But I did NOT add anything to manifest as explained by this link
layout-land xml files does not work with onConfigurationChanged call back
First thing you must check is whether you already have in your Manifest.xml android:configChanges="orientation" that means that you wish to control the state of your activity when the orientation changes. When there's an orientation change Android will destroy your activity and recreate new, so any data will be lost.
So you should use onConfigurationChanged() method or onRetainNonConfigurationInstance() method.
I recommend to you read some tutorial.
I have created two layout files (in res/layout and res/layout-land) for my application. But it is not displaying the proper layout file when i change the device orientation. It just displays the same layout file (res/layout) in both the modes. Why it is not picking up the landscpe mode layout file in landscape mode? Also i have added android:configChanges="keyboard|orientation" to my activity in manifest. What else i have to do? I'am a newbie to android app development.
If you are adding added android:configChanges="keyboard|orientation" in Manifest file it won't change the layout, because by adding this attribute your Activity is not re-created again so it displays the same layout as in portrait mode. So, remove android:configChanges="keyboard|orientation" from Manifest file to get it working.
If you want that your Activity should not be created again and also it should change the layout when your orientation changes you have to add code for changing the layout manually in onConfigurationChanged() check my answer here for getting it working.
You should not add this.
android:configChanges="keyboard|orientation"
in your manifest.. because it is not allowing the call for onConfigurationchanged method... which is responsible for handling the orientation changes... remove that line from you manifest and it should work...
I am a starter on Android and trying to figure out how do use different layout files (main.xml) for portrait and landscape screens.
I tried to put main.xml in both res/layout and res/layout-land, then rebuild the project. However, this doesn't work for me.
I then added onConfigurationChanged method in the code and setContextView to R.layout.main. This time I got different screen layout in different orientation, but all functionality were lost. Some posting also said using onConfigurationChanged method is not really the best idea.
So can anyone help me with this issue?
Thanks,
Go to your AndroidManifest.xml and add this line to your activity tag:
android:configChanges="orientation"
This tells Android that you wish to control the state of your activity when the orientation changes. Normally, when there's an orientation change Android will actually destroy your activity and recreate it by mean of onCreate() again, so any data that was obtained will be lost unless the state is saved in the onConfigurationChanged() method.
Android will automatically use the layout that represents your devices current state. For your specific project, the layout-land xml file should be used when the device rotates. When you override the onConfigurationChanged method and modify your manifest, it tells Android you will handle the configuration change yourself which I do not recommend, especially if you want a new layout to be loaded.
Here's what you need to do (to test for the different layouts). Put an xml file called main in your layouts folder. For the layout, just use a LinearLayout that fills the parent with a blue background. Now create an xml layout file called main but put it under the layout-land folder. In this layout, use a red background.
Start the app, rotate the device, and you should see the background change colors. This is because the activity is being destroyed and restarted with the new layout.
Try to use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() methods to solve your problem.
Also you can use above sloution given by jrobinson3k1.
Thanx.
Hi
M new to android.I faced a problem i.e in landscape mode i need a layout view which doesnot have some feilds which are present in the portrait mode.I have created a layout for landscape view in such a way.In the activity i have given the conditions like if portrait display some feilds some hide.its working properly actually...first when i go from portrait to landscape it is giving what i want...but when i go to portrait mode its stopping the application.please help me.Thanks in advance
You can make two different layouts and put them in layout-port and layout-land respectively.
And make sure that you have mentioned android:configuration = "orientation" in your manifest for that activity. Try it if i have understood you correctly.
I think that the advice by Kantesh may be backwards. As explained in the docs, if you include android:configChanges="orientation" in the manifest, then the correct resource (from layout-port or layout-land) won't be automatically loaded. Instead, leave out mention of orientation from the manifest. Then, you do not need to worry about onConfigurationChanged() (don't override it). The system will automatically shut down the activity and then restart it, binding the appropriate version of configuration-dependent resources. Handling configuration changes yourself is (again, according to the docs) only a last resort to deal with performance issues that cannot be handled in other ways.