Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android screenorientation change for all activities

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?

like image 407
PVoLan Avatar asked Sep 16 '11 09:09

PVoLan


2 Answers

No way. I need to repeat

<activity android:name=".LauncherActivity"   android:configChanges="keyboardHidden|orientation"    android:screenOrientation="portrait"> </activity> 

in every <activity> tag in manifest.

like image 190
PVoLan Avatar answered Oct 04 '22 13:10

PVoLan


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

like image 34
mthpvg Avatar answered Oct 04 '22 13:10

mthpvg