16

Possible Duplicate:
How can I set Orientation Fixed for all activities

I would like to lock my screen orientation for my app. I mean that all Activities must have the same landscape orientation. I don't want to add android:screenOrientation="portrait" in the manifest for all activities. Is there any other way? thanks Regards

Community
  • 1
  • 1
Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61
  • The thing which you want is being fulfilled by **screenOrientation** then why do you want any other Option... :| – Bhavin Oct 12 '12 at 13:14
  • Can you please explain why you don't want it in manifest ? – Murtuza Kabul Oct 12 '12 at 13:14
  • Why not you lock it at runtime? Please elaborate exactly what you want. – Hassan Jawed Oct 12 '12 at 13:17
  • 1
    Duplicate of http://stackoverflow.com/q/6582761/1321873 – Rajesh Oct 12 '12 at 13:17
  • I want some code or part of code , that I must to add in the manifest , but I don't want to add some code for all activities , also I want to know is there any normal way , for example my app has 40 different activities and it's not normal to add some code for all these activities 40 times:) – Hayk Nahapetyan Oct 12 '12 at 13:19
  • Dear Rajesh I need some code for manifest , tell the truth I'm new in android , and I think it's not normal if there not any way to do this without any class helper , or add row of code for all activities. I think it Should have some easy way – Hayk Nahapetyan Oct 12 '12 at 13:30

2 Answers2

26

You have two options for achieving the result.

  • You can specify per activity base orientation requirement in manifest file. You can select here what orientation you want to support for which activity.

e.g.

<activity
    android:name=".Some_Act"
    android:label="@string/app_name"
    android:screenOrientation="portrait" >
</activity>
  • You can create a base activity and specify the orientation in that activity. Later you can inherit that activity in other activities of your application. You can choose suitable base class to get the desired behavior.
Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • 2
    What you would insert into the base activity: `setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);` – levibostian Apr 13 '16 at 19:05
  • 7
    second one - is bad option. cos if phone in landscape orientation - it will start landscape firs, and than will be recreated! if you have some methods in onCreate - it will run twice. so my advise - use first method (manifest one) – Andriy Antonov Jun 05 '17 at 11:15
  • If you want to avoid the problem explained in the comment above, check my answer in the linked question that allows locking orientation for all activities at build time: https://stackoverflow.com/a/46935264/245966 – jakub.g Oct 25 '17 at 14:41
1
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"

Add the above lines in your app's manifest for all the activities .

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
Goofy
  • 6,098
  • 17
  • 90
  • 156