Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent my Kotlin app screen from rotating?

Tags:

android

kotlin

I've made a Kotlin app and I don't want the app's screen to rotate. I know that with Java, I can just put

android:screenOrientation="portrait"

in the AndroidMainfest, but how about Kotlin? How can I do it?

like image 243
Mohammed Babełły Avatar asked Sep 04 '18 01:09

Mohammed Babełły


People also ask

How do I stop my screen from automatically rotating?

Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.

How do I restrict orientation on Android?

Add android:screenOrientation="portrait" to the activity you want to disable landscape mode in.

Why are my apps rotating?

If you've enabled auto-rotate, your Android phone's screen is supposed to rotate when turn your phone 90 degrees or more. Sometimes, though, you might notice that this doesn't happen. It's possible a third-party app, a system glitch, or a faulty sensor is causing your phone not to recognize the movement.


Video Answer


2 Answers

In MainActivity you could use ...

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        **requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;**
like image 163
Valery Lavrov Avatar answered Oct 26 '22 14:10

Valery Lavrov


AndroidManifest is a .xml file. You don't write Kotlin code in there. So you can still use:

<activity android:name=" example.com.YourActivity"  
     android:screenOrientation="portrait">  //or "landscape"
</activity>   

This answer could be helpful for you too How to set entire application in portrait mode only?

like image 20
Ro Ra Avatar answered Oct 26 '22 13:10

Ro Ra