2

i have one xml file called activity_main.xml in both orientation portrait/landscape. The problem is that xml is with same name but tags inside are completly different. Is it possible to force activity when enter at OnCreate method, based on some criteria to use only portrait mode. Setting screen orientation in Manifest dont work for my situation.

I tried this:

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

The problem comes when user try to open activity in landscape mode etc phone is in landscape mode before open MainActivity and setContentView(R.layout.activity_main); tried to get activity_main.xml from land folder but i want to get view from portrait, is it possible or my logic is wrong.

Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37

3 Answers3

1

Create layout-land.xml and its will called Automatically when orientation landspace

Either method forces the direction setting is by use screenOrientation tag in activity tag at AndroidManifest

android:screenOrientation="landscape"

or using next method

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
5ec20ab0
  • 742
  • 6
  • 15
  • Nope i write it in my question -. "Setting screen orientation in Manifest don't work for my situation". It need to be setted programmatically. – Kristiyan Varbanov Aug 27 '18 at 14:11
0

You can use this function:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

https://stackoverflow.com/a/50616559/5551621

Tomas Jablonskis
  • 4,246
  • 4
  • 22
  • 40
Venkata Narayana
  • 1,657
  • 12
  • 25
  • How about this? super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) setContentView(R.layout.activity_main); – Venkata Narayana Aug 27 '18 at 14:04
  • I tried but same situation, again call xml layout from land folder which is very strangely. – Kristiyan Varbanov Aug 27 '18 at 14:06
  • Hey @KristiyanVarbanov, same issue here. Causing crashes since the setup flow expects certain views which are only in the portrait xml. Did you figure this out? – Avramo Mar 23 '22 at 12:52
0

You can use the following code to force activity to be only in portrait mode

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Riddhi Shah
  • 3,092
  • 5
  • 36
  • 56
  • @KristiyanVarbanov I've checked your code. it's working Perfectly. In my device – Riddhi Shah Aug 27 '18 at 14:16
  • Did you tried before open your activity to rotate device in landscape mode and then to open the activity? – Kristiyan Varbanov Aug 27 '18 at 14:18
  • First, I opened another activity in landscape mode after a click I opened this activity (in landscape mode only) but after applying that line of code the activity opens in portrait mode (i.e. opens portrait mode xml) – Riddhi Shah Aug 27 '18 at 14:21
  • Can you show me full code? I mean have you added anything related to this? That is not mentioned in the code? – Riddhi Shah Aug 27 '18 at 14:22