Screen orientation of an android application - android

Is it possible to write code on android manifest file that will automatically change screen orientation according to the device screen size?.

May this help you:
Instead of fixing the orientation on the manifest, you could use setRequestedOrientation. On the onCreate method of each activity, check if you are on a tablet or smartphone and set the desired orientation.
Another option could be Creating Multiple APKs for Different Screen Sizes, depending on your needs.

As far as I know this cannot be done in the manifest. However, you can write some code to achieve this. Have a look at the setRequestedOrientation method of Activity. Details in the documentation.
However, this will probably not generate the best user experience.

AFAIK Tablets have default orientation set to Landscape.
So this should automatically handled

Write line given below in your defined Activity in Androidmanifest.xml,
android:screenOrientation="unspecified"

Related

how to fix orientation issues on android

I wanted to ask about android orientation;
I made 2 different folders for layout: layout and layout-land
when the orientation changes while the application is running i don't want to restart the running activity so i fixed that problem by adding the following line of the manifest file
android:configChanges="keyboardHidden|orientation|screenSize"/>
the problem then is that the app is not switching to the appropriate layout when i rotate the phone
does someone have any idea about the best way to handle the orientation changes of an android application ( i want to keep the state of the layout when i switch )
Simple: Let Android do the hard work.
Saying configChanges="orientation" overrides the default orientation behavior especially if you're using multiple layout files.
Change this:
android:configChanges="keyboardHidden|orientation|screenSize"/>
To this
android:configChanges="keyboardHidden|screenSize"/>

putting whole application in landscape mode in manifest

The line android:screenOrientation="landscape" in each activity in manifest file will put that activity in landscape.
Is there any way to put whole application in landscape in manifest? I put it in application field but didnt work.
I have an app which will only use landscape mode.
Nope. You might somehow embed it into the code before you launch your activities, but overall, the activities specification must have that setting. Look at the specification here and here.

Android Rotation Problem with onConfigurationChanged

I gave android:configChanges="orientation|keyboard" to my activity in manifest and when I rotate my device, onConfigurationChanged is always called. No problem with it.
What I want is when device is rotated, font size of widget become changed.
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/my_font_size" />
Like above, textSize attribute refer the value in resource
and the xml file defining my_font_size is in values-land, values-port folders.
Ok. I'm ready. Build it, run it, and rotate the device, and there is no change.
requestLayout() in onConfigurationChanged() doesn't work.
Any advise about it?
Thanks.
You shouldn't use configChanges attribute unless its absolutely necessary (this attribute is introduced to enable some performance-sensitive scenarios). Please carefully read Handling Runtime [Congiuration] Changes.
This technique [using configChanges] should be considered a last resort and is not recommended for most applications.
Having warned you, here is explanation why you do not see changes when rotating the device.
If you do not use configChanges="orientation" on your activity, you activity is destroyed and recreated with resources for new orientation. In this case your code in onCreate() will automatically pick values from correct resources (*-land or *-port).
If you do use configChanges="orientation" you basically saying to Android to not apply changes automatically, and that you will do this by yourself in onConfigurationChanged().
Assuming you really need to keep configChanges, here is what you can do to fix issue with font size:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
int fontSize = getResources().get(R.id.font_size);
mTextView.setTextSize(fontSize);
}
But again, you really should not use configChanges in most cases.
If you specify android:configChanges="orientation|keyboard" in your activity then Android won't handle this changes for you (meaning that it won't recreate your activity using the proper resource files). That's why you don't get the change in the font size. If you want Android to handle this, then don't add the configChanges to your Activity in the Manifest.
You can actually specify two different layout files for the two layout types, and configure each with what you want.
Some documentation: Application Resources
If all you want is to make cosmetic design changes based on orientation that is probably the preferred method

android layout-land and onConfigurationChanged

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.

screen orientation

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.

Categories

Resources