Mohammed Babełły
Mohammed Babełły

Reputation: 183

How can I prevent my Kotlin app screen from rotating?

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?

Upvotes: 13

Views: 9914

Answers (5)

Nikita Ghori
Nikita Ghori

Reputation: 21

Prevent your kotlin activity from rotating to write this is in your AndroidManifest.xml file

 < activity android:name=".activity_name" 
         android:screenOrientation="nosensor" >< /activity>

Upvotes: 2

Valery Lavrov
Valery Lavrov

Reputation: 121

In MainActivity you could use ...

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

        **requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;**

Upvotes: 2

Chirag Nahar
Chirag Nahar

Reputation: 159

In Kotlin you can write same code which is you written in java because manifest file is written in xml file so you have to put just these line in activity Tag.

android:screenOrientation="portrait"

Upvotes: 3

Ro Ra
Ro Ra

Reputation: 325

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?

Upvotes: 9

Jitendra A
Jitendra A

Reputation: 1658

It does not matter whether you use java or kotlin. The android sdk is a different thing and you should not confuse that with programming language. Kotlin code looks different from java because of syntactical differences in language. Under the hood both are the same bytecode.

Your android manifest is written in xml, its just like a configuration file and is not affected by your choice of programming language for the code. So, their is no need to change anything in the manifest file. You, can directly use :

android:screenOrientation="portrait"

Upvotes: 6

Related Questions