I want to fix screen orientation on my android application to portrait. Using google, i've found, that this can be done by adding the following code to my manifest.xml:
<activity android:name=".LauncherActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait"> </activity>
That's fine enough, but the problem is follows: I've about 15 activities now, and this number will grow. Is there any way to apply orientation to all activities at once?
I tried to use styles in a following way:
theme.xml
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="Custom" parent="android:Theme"> <item name="android:screenOrientation">portrait</item> <item name="android:windowBackground">@drawable/launcher</item> <item name="android:configChanges">keyboardHidden|orientation</item> </style> </resources>
Manifest.xml
<application ... android:debuggable="true" android:theme="@style/Custom" > //... </application>
or
<activity ... android:theme="@style/Custom" > //... </activity>
Both the examples apply windowBackground
succesfully, but screenOrientation
does't works. Any ideas?
No way. I need to repeat
<activity android:name=".LauncherActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait"> </activity>
in every <activity>
tag in manifest.
You can create a class which extends Activity
:
public class PortraitActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } }
And I think that if your activities extends PortraitActivity
instead of Activity
it will do.
EDIT : SCREEN_ORIENTATION_PORTRAIT or SCREEN_ORIENTATION_LANDSCAPE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With