10

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?

Pang
  • 9,564
  • 146
  • 81
  • 122

5 Answers5

6

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"

Jitendra A
  • 1,568
  • 10
  • 18
6

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?

Ro Ra
  • 265
  • 2
  • 8
3

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"

Chirag Nahar
  • 159
  • 8
2

In MainActivity you could use ...

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

        **requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;**
Valery Lavrov
  • 91
  • 1
  • 3
  • Please explain what your code does and how it does it. – M-Chen-3 Dec 26 '20 at 21:40
  • prevent app screen from rotating in Kotlin. that was a question. The rest of it self explanatory. The reason is not done in XML so it is part of the logic and could be called when it needed and canceled right after. – Valery Lavrov Dec 27 '20 at 23:10
2

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

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