How to keep the screen in landscape mode in Android - android

I have an activity which should always be displayed in Landscape mode. So i added android:screenOrientation="landscape".
But the problem is when I rotate the device by 180 degrees, the display is inverted. is there a way to handle this issue so that the screen elements are always shown correctly.?

Actually what you really want is to specify:
android:screenOrientation="sensorLandscape"
in your AndroidManifest.xml. This will listen to the sensor data while snapping between landscape and reverseLandscape.

Others have mentioned sensorLandscape...to do this programmatically in your activity (or a base activity), you can set your orientation to that:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
I'm doing it in onResume(). This will respect landscape and reverse landscape when you flip the device around 180 degrees in the middle of the activity, without having to use onConfigurationChanged().
This was helpful to me since for tablets I need landscape/landscape reverse only, and for phones I need portrait/portrait reverse only, and don't want to do two separate AndroidManifest files. ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT also exists.

So just for everyone information, this is what i did.
In Android manifest added android:screenOrientation="landscape".
In on resume method add these lines of code
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getRotation();
if(orientation==Surface.ROTATION_180)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
This way my screen is always positioned correctly even if user holds the device upside down.

In the Android Manifest write this:
android:configChanges="orientation"
android:screenOrientation="landscape"
like in the example below.
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

The good method is to use
setRequestedOrientation(6);
6 is the value for sensorLandscape but it seem's that there is no defined constant for it.

Hmmm. This depends a bit on the framework version you're using as well. Try this ast a start:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
If that works great. If it dosen't you have to tell us a little more about the layout of your app.

Related

How to set the Portrait and landscape orientation in android manifest file

Portrait mode is working and landscape is not showing.
android:screenOrientation="portrait" // Here i need to show both portrait and landscape.
You can use:
android:screenOrientation="unspecified"
This is the default value and the system chooses the orientation.
alternatively, you can use:
android:screenOrientation="fullSensor"
In this case, device orientation sensor determinates the screen orientation.
Try one and the other to see which one works best for you.
EDIT:
For a specific class, you can do this:
<activity
android:name=".YOUR_CLASS"
android:screenOrientation="unspecified" // or what you want
android:theme="#style/Theme.Design.NoActionBar"/>

How to keep the android screen size?

When my android phone is landscape,the screen size change,how to stop it?I kown that had edit manifest "configChanges",activity not start itself life again,but the display size had chang.
Just add in AndroidManifest.xml in activity tag:
<activity
android:name="MyActivity"
android:screenOrientation="portrait"/>
Add android:screenOrientation="portrait" to the element/s in the manifest . This will solve your problem. Or if you want your activity in always landscape mode you can add android:screenOrientation="landscape"

android:#Theme.Translucent Locks orientation to Portrait?

I've created a new style to set the background as transparent, but for some reason ever since the app doesn't change layout to landscape when device is rotated.
Should I specify something in the new style ?
Here is it's code :
< style name="Transparent" parent="android:#Theme.Translucent">
< item name="android:windowBackground">#color/background</item>
</style>
While you are not specify android:screenOrientation for activty, android uses
"unspecified" default value, which means that the system chooses the orientation.
In a case you are using style with <item name="android:windowIsTranslucent">true</item>, system selects an orientation based on the orientation of underlying activity in stack. So it's hard to get your application in landscape mode, because most of launchers fixed to portrait.
Just set android:screenOrientation="sensor" for all Translucent activities to force it choose orientation independently:
<activity
android:name=".SomeActivity"
android:screenOrientation="sensor"
...>
...
</activity>
I actually kinda solved it apparently the orientation is according to the background app in case of transparent background, since i kept testing when the Launcher in the back, i always got portrait only.
just right in manifest
<activity
android:name=".Ippin_NewActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name" >
</activity>

Android : Realign layout after screen keyboard auto suggestions complete

I hope that makes sense...
Basically when the on screen keyboard appears and then the auto suggestions as well, it pushes my view up the screen - a graphic in an imageView that I would like to persist.
When the auto complete is over and done with, the screen stays where is is despite the auto suggest disappearing.
Is there any way to let my layout reclaim that space and simply go back down again into the empty space ? ie to float back down again ?
Cheers :)
If I got you right, try adding the following line to the <activity> in your manifest:
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
...
android:windowSoftInputMode="adjustPan"
...>
...
</activity>
...
</application>

How to block the screen orientation and still get orientation changes notifications

I want to block my application orientation to portrait, but still receive orientation changes events. So I've tried to add the android:screenOrientation="portrait" attribute for blocking the orientation and the android:configChanges="keyboardHidden|orientation" attribute for receiving the orientation changes events in the ovverriden method onConfigurationChanged. Still the method doesn't get called. Is there a way to achieve this?
By forcing the orientation of your activity to portrait mode, you're not able to receive orientation changes.
Check Manifest.xml file. It should be like as below:
<activity android:name="YourActivity1"
android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"
android:configChanges="orientation|keyboardHidden"></activity>
<activity android:name="YourActivity2"
android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"
android:configChanges="orientation|keyboardHidden"></activity>
are you sure you write this for main activity like
< activity android:screenOrientation="portrait"...

Categories

Resources