How to lock ImageView orientation in Activity?

164 Views Asked by At

How to lock ImageView orientation when rotate screen in Kotlin? simply how to freeze ImageView (as background) when rotate activity?

Trying: I tried to change a image in LinearLayout as background, and use this method but it is not working!!

override fun onConfigurationChanged(newConfig : Configuration) {
    super.onConfigurationChanged(newConfig)
    val linearLayout = findViewById<LinearLayout>(R.id.linearBg)
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        linearLayout.setBackgroundResource(R.drawable.bg_landscape)
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        linearLayout.setBackgroundResource(R.drawable.bg_portrait)
    }
}
1

There are 1 best solutions below

0
Semyon Moroshek On BEST ANSWER
val screenOrientation = this.resources.configuration.orientation
    if (screenOrientation == Configuration.ORIENTATION_PORTRAIT) {
        // TODO
    } else {
        // TODO
    }