I am trying to lock an activity to landscape mode and have an overlying activity receive and use the orientation changes. The top activity is partly transparent so it is important that the bottom activity locks and is displayed correctly. I have tried to set it programatically with setRequestedOrientation() and in the AndroidManifest.xml with android:screenOrientation="landscape"
When I lock the orientation using the manifest the top activity also locks. When I add android:configChanges="orientation|screenSize|keyboardHidden" the top activity changes orientation but so does the bottom activity. Sometimes the bottom activity goes completely blank (transparent) after the orientation changes.
This is in my AndroidManifest.xml:
<application
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TopActivity"
android:screenOrientation="sensor"
android:configChanges="orientation|screenSize|keyboardHidden"
android:theme="#style/Theme.Transparent">
</activity>
</application>
MainActivity is the bottom activity. How do I properly handle the orientation changes so that the single activities are locked the way I set them to?
just add screenOrientation to the activity which you don't want to rotate on orientation change in the manifest file like this:
<activity android:name=".Activity"
android:screenOrientation="portrait" />
All other activities will automatically rotate on orientation change.
No need to give screenOrientation for other activities.
1) To disable orientation (i.e. to prevent Android from restarting your activity if the user flips the phone), then specify "configChanges" in your AndroidManifest (like you're already doing, apparently), and then simply don't respond to any changes:
<activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation|screenSize">
2) Alternatively, let's say you want to force your app to "landscape" mode. Instead of "configChanges", you can specify "screenOrientation"
<activity android:screenOrientation="landscape" ... >
3) Finally, if you want to handle the orientation change, you'll need to a) save state, then b) do whatever processing you need (including, perhaps, notify other activities):
http://developer.android.com/guide/topics/resources/runtime-changes.html
Related
I have an app which navigates between 2 activities. Both activities initialize the requested orientation to the sensor:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
and I don't have any overrides in the layout files:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainListActivity"
android:background="#drawable/mainbg"
android:scaleType="centerCrop">
and my manifest is the same:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.domain.project">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity android:name=".OneOfMyActivities"></activity>
<activity
android:name=".MainListActivity"
android:label="#string/title_main_list"
android:theme="#style/AppTheme.NoActionBar.NoActionBar" />
<activity
android:name=".DetailActivity"
android:label="#string/title_detail"
android:parentActivityName=".MainListActivity"
android:theme="#style/AppTheme.NoActionBar.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="my.domain.project.MainListActivity" />
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The app functions correctly as far as updating orientation when you rotate the device. If you take the device and rotate it into landscape mode, the screen correctly swaps to landscape, and stays there. However, when you navigate to the other activity in the app, the other activity displays in portrait mode, and then immediately shows the rotate animation before going to landscape. So, it obviously knows which orientation it should be in. Why does it go from landscape to portrait and back to landscape? Is there any way to have it remember that it's already in landscape, and initialize the new activity in landscape?
Having the screen jump and rotate every time you navigate, when the user is not moving their device is rather annoying.
I've tried googling for this, but all I seem to find are answers about how to lock screen orientation, or questions asking why the activity restarts when the screen orientation changes. Neither of which are my question. I want to still allow the app to change screen orientation when the user rotates the device, I just want it to remember that state when navigating between activities.
thank you.
You can try putting this
android:screenOrientation="fullSensor"
In your activity tag and remove the then redundant
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)
I have application with screen rotation.
rotating mechanism i handle with configChanges:
<activity
android:name=".ui.activity.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"
android:screenOrientation="fullSensor"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Problem:
When user turn off screen, rotate device on 90 degrees and unblock it screen start with "old" orientation of my app, it look ugly.
here scheme:
after block and rotate i get this
blink and app come to normal
try it on Nexus 5(android v5) and Nesus 10(android v4.4), at phones all works fine
Adding android:configChanges="orientation|screenSize" tells the system that you are going to handle these config changes yourself, and for the system to not do anything about it.
By removing them, your activity will be recreated with the correct layouts when you rotate.
If you're using configChanges to avoid the activity being recreated, I feel bad for you son.
My app is done in Titanium. I am struggling with a issue which is that when I= launch the App by keeping the device in landscape mode. Then while loading Splash screen it first goest in Landscape mode and when app finally launches Whole UI got distorted by that orientation change. I have added the below line of code in TiApp.xml
<application android:screenOrientation="portrait" android:configChanges="keyboardHidden"/>
even though its rotating. I want to stop the orientation change of that splash screen while launching. Any idea?
In each activity you have to write
<activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="landscape" />
Hope it help!!
Try this. Add you custom androidManifest.xml For custom xml
and add this line in androidManifest.xml and run I think this is very useful for you.
<activity android:configChanges="keyboardHidden" android:name="org.appcelerator.titanium.TiActivity" android:screenOrientation="portrait"/>
<activity android:configChanges="keyboardHidden" android:name="ti.modules.titanium.ui.TiTabActivity" android:screenOrientation="portrait"/>
Cheers
I want to lock horizontal view in my app, is it possible?
Help is highly appreciated,
Thanks,
Use android:screenOrientation="landscape" attribute for your activity.
add this code in your menifest...
<activity android:name=".activity"
android:configChanges="orientation" android:screenOrientation="portrait"></activity>
For new android Studio 3 and above
you need to put these lines in AndroidManifest.xml code if you want to set the orientation to portrait
One thing this is sensor portrait, means both upside - downside will work.
But if you only want to put portrait for upside then, replace sensorPortrait to portrait
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="sensorPortrait"
tools:ignore="LockedOrientationActivity">
Same goes for only landscape too
just one parameter change portrait to landscape
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation= "sensorLandscape"
tools:ignore="LockedOrientationActivity">
I am wondering how I can make the orientation of the screen permanent, even when the phone has a slide-out keyboard. This is the code I have:
<activity android:name=".AppName" android:label="#string/app_name"
android:screenOrientation="portrait">
Thanks.
Try adding:
android:configChanges="orientation|keyboardHidden"