Force use only portrait xml layout android?

Force use only portrait xml layout android?



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.


MainActivity


setContentView(R.layout.activity_main);





Put your xml here
– Abner Escócio
Aug 27 at 13:53





Why you have an xml landscape then? Just dont use it!
– Abner Escócio
Aug 27 at 14:16





It need to be used but in other situations.
– Kristiyan Varbanov
Aug 27 at 14:19




4 Answers
4



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


setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);





I edit my answer, please check it.
– Kristiyan Varbanov
Aug 27 at 14:01





@KristiyanVarbanov I've checked your code. it's working Perfectly. In my device
– Riddhi
Aug 27 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 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
Aug 27 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
Aug 27 at 14:22



You can use this function:


setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);



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





I edit my answer, please check it.
– Kristiyan Varbanov
Aug 27 at 14:01





How about this? super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) setContentView(R.layout.activity_main);
– Venkata Narayana Malireddy
Aug 27 at 14:04






I tried but same situation, again call xml layout from land folder which is very strangely.
– Kristiyan Varbanov
Aug 27 at 14:06



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


screenOrientation


AndroidManifest


android:screenOrientation="landscape"



or using next method


setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);





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 at 14:11



you can add


<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"/>



or if you want your whole app the be portrait instead of re writing the same code for every activity user


android:screenOrientation="portrait"



inside application in manifest





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 at 14:13






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế