How do I programmatically disable screen rotations for an entire Android application?
As in keep it in portrait or Landscape no matter which way device is tilted?
You need to add this to your manifest inside the activity that you want to limit
android:screenOrientation="portrait" //Or landscape for horizontal.
If you want it different between activities just make your manifest look like this.
<activity android:name=".Activity1"
android:screenOrientation="landscape">
</activity>
<activity android:name=".Activity2"
android:screenOrientation="portrait">
</activity>
<activity android:name=".Activity3"
android:screenOrientation="landscape">
</activity>
Fix the orientation in android manifest for every activity
<activity android:name=".SomeNewDesignActivity"
android:screenOrientation="portrait" ></activity>
Too late to the party!
Anyway if you want to disable rotation without forcing the user to choose landscape or portrait you can use in each activity in the manifest
android:screenOrientation="locked"
So your manifest should look like this
<activity android:name=".Activity1"
android:screenOrientation="locked">
</activity>
<activity android:name=".Activity2"
android:screenOrientation="locked">
</activity>
<activity android:name=".Activity3"
android:screenOrientation="locked">
</activity>
So the app will keep the current orientation the user decided to use.
The same behaviour can be obtained programamtically:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Best way to do screen orientation to potrait/landscap for entire app -
Add following lines in your app theme -
for making all activity portrait -
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:screenOrientation">portrait</item>
</style>
for making all activity landscape -
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:screenOrientation">landscape</item>
</style>
Related
Currently I have a very low-res splashscreen I made by setting the applications theme to a drawable image. This would work PERFECTLY if it wasn't for this. Now I'm looking for an alternative.
The one thing I will not do is create a timed splashscreen, which there are plenty of guidelines on how to make one, the one thing I want to know is how do I create a splashscreen that will load first thing, then finish once MainActivity has finished loading?
The one thing I love about my current splashscreen is that it loads instantly when the app starts, but it will cause major delays when clicking buttons if it's high-res (more than 300x300pixels).
Here's my current code for the flawed, laggy splashscreen that loads and stops based on MainActivity's loaded state:
in styles.xml:
<style name="splashscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">#drawable/splashscreen</item>
</style>
in manifest:
in AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:fullBackupContent="false"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:theme="#style/splashscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Check for possible issues with overdraw. It seems that the background (the splash image) is still there. see http://www.curious-creature.org/2012/12/01/android-performance-case-study/ if it applies to your case.
Removing the window background: the background defined in your theme is used by the system to create preview windows when launching your application. Never set it to null unless your application is transparent. Instead, set it to the color/image you want or get rid of from onCreate() by calling getWindow().setBackgroundDrawable(null).
Most of google applications (e.g Google Maps, Google sheets, etc...) have a pretty nice splashscreen, that pops up very quickly.
It doesn't look like a "classic" Android splashscreen made of an Activity launching another one after xx secs.
It makes me think of the iOS equivalent (Launch images).
Is it a new UI element that we can use ? Does somebody have an hint about that ?
You have to use a theme for your SplashActivity instead of directly setting the image in the layout.For eg:
<style name="Theme.Splash" parent="AppTheme">
<item name="android:windowBackground">#drawable/ic_splash</item>
</style>
Then apply this theme to your splash activity in Manifest.for eg:
<activity
android:name=".controller.activities.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/Theme.Splash"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I wanted to add a SplashScreen to my app and did a little research on the matter. Some tutorials said that you could create an activity and with some timers show it for a few seconds. I couldn't get them working, and then in this page How do I make a splash screen? the second top voted answer said that instead of showing an activity (and because of that wouldn't substitute the white/black launch loading screen but instead add more delays) you should instead create a custom style and assign it to your activity in the Manifest file. I did that, created a new Style like this:
<style name="splashScreenTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/launchscreen</item>
</style>
in the styles.xml and changed my Manifest to this:
<application
android:name=".EFBApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivityDrawer"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/splashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When I run my app I can see the launch screen perfectly but then it crashes. By breakpoints I discovered that when the MainActivityDrawer (my main class) gets to the super.onCreate(savedInstanceState); line it then crashes (it goes to the ZygoteInit.java class and after that it crashes while breakpoint debugging). If I take away the android:theme lines in the Manifest it works fine but shows the horrible plain white screen while launching. Any suggestions or ideas? Thanks a lot.
Ok, so I managed to make it not crash, instead of assigning the parent as Theme.AppCompat.NoActionBar, I used just AppTheme and it works now. I hope this helps anyone.
I have meet a problem, there is a tabhost in my app.which have 4 tabs,each tab have more than one activiy.I just want to fix some of activities in portrait mode while other's can change orientation mode,any suggestions?
<activity android:name=".MyTabHost"
android:windowSoftInputMode="adjustPan|stateVisible"
android:configChanges="keyboardHidden|orientation"
></activity>
<!-- android:screenOrientation="portrait" -->
<activity android:name=".Products_Images"
android:configChanges="keyboardHidden|orientation"
></activity>
<activity android:name=".Products_Upload"
android:screenOrientation="portrait"></activity>
<activity android:name=".Products_Description"
android:screenOrientation="portrait"></activity>
in products_images.java,i just want to show larger image while orientation changed,however in products_upload.java and products_description.java,i just want to fix orientation in portrait mode, all three class just in one tab.but it doesnt work!!
use this in activity tag in manifest file:
android:screenOrientation="portrait"
Using phones that have android 2.1 & 2.2 installed, using the simplest case of a hello world app and add android:theme="#android:style/Theme.Translucent" to the activity in the android manifest to have the app be transparent, the app sticks as portrait only and won't rotate to landscape when the phone is rotated.
Take the line out and the app rotates ok. This is verified by adding the override of onConfigurationChanged and putting a breakpoint in that routine. Brk hits when translucent isn't applied, doesn't when you add translucency.
However, using a samsung galaxy tab using andr 2.2, rotation works ok even with translucent applied. Anyone have any ideas on this?
I had a same problem. Just add android:screenOrientation="sensor" in the manifest file after you specify theme:
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
So far I tested it on android 2.2 and 4.1 - works as expected.
I have a same problem... but in my case I used Translucent because I solve redrawn warning (this warning appear when set color on android:background)
I solved the warning creating a Theme with parent Theme.Lignt and rewrite two attributes
Something like this
<style name="MyTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/my_background</item>
<item name="android:colorBackground">#color/my_background</item>
</style>
If you need use Translucent in ApiDemos has a sample when an activity have a translucent theme and orientation service works well