Skip to content Skip to sidebar Skip to footer

How To Change Screen Orientation, Without Creating A New Activity On Android?

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 i

Solution 1:

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: landscapeportraitsensor 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><integername="orientation">1</integer>  // 1 for portrait
</resources>

for values-large/integers.xml:

<?xml version="1.0" encoding="utf-8"?><resources><integername="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.

Solution 2:

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! ;)

Solution 3:

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

<activityandroid:name=".CoreActivity"android:screenOrientation="behind"/>

This will use the same orientation as the activity that's immediately beneath it in the activity stack.

Solution 4:

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.

Solution 5:

try this in your manifest.xml... this will stop the recalling the onCreate() multiple time while orientation changes...

android:configChanges="keyboardHidden|orientation|screenSize"

Post a Comment for "How To Change Screen Orientation, Without Creating A New Activity On Android?"