Home > Mobile >  How to make android landscape layout work without refreshing activity?
How to make android landscape layout work without refreshing activity?

Time:03-09

I have seen answers regarding this with removing android:configChanges for the landscape layout to work but then this will refresh the activity and I need to avoid that. Some answers suggest programmatically handling config changes but I'm not sure how to do that. Should the landscape layout be created programmatically? Does anyone have any idea how to solve this?

CodePudding user response:

The correct answer is to remove the android:configChanges from your manifest, and correctly handle the activity being restarted. There's techniques to fix all the problems you may have with it, and there's dozens of other things that can cause it to happen anyway other than rotation changes.

If you absolutely must handle it yourself you can do so by overriding onConfigurationChanged and checking to see if it was the orientation that changed. But you don't want to do that. If you try to do it yourself, you're going to have all the bugs you cause by doing it yourself incorrectly and all the bugs you didn't fix because you aren't handling activity restarts correctly. So forget about this, remove the configChange from your manifest, and fix it so you cn handle restarts. Because your app is going to be a buggy mess until you do that, with more crash reports than you can imagine.

CodePudding user response:

you can make new Layout file for this, with the same name your layout and choose Orientation in available qualifiers. and Android manages everything.

enter image description here

CodePudding user response:

If you dont want the activity to be recreated, then you have to use android:configchanges with orientation. You will have to handle the layout programmatically in onConfigurationChanged method. It can be complicated since you are not allowing system to handle it. You will have to set the alignment,parameters and all other structure programmatically. You will get reference of existing portrait mode elements and have to change their position and other things yourself.

CodePudding user response:

You can listen for orientation changes using OrientationEventListener,

Here is a sample code in kotlin which will listen for orientation change and check it is landscape or portrait mode,

private var mOrientationEventListener: OrientationEventListener? = null

private val THRESHOLD = 30

private var isLandScape = false

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mOrientationEventListener = object : OrientationEventListener(context) {
        override fun onOrientationChanged(orientation: Int) {
            isLandScape = isLandscape(orientation)
        }
    }
    mOrientationEventListener!!.enable()
}

private fun isLandscape(orientation: Int): Boolean {
    return orientation >= 90 - THRESHOLD && orientation <= 90   THRESHOLD || orientation >= 270 - THRESHOLD && orientation <= 270   THRESHOLD
}

override fun onDestroy() {
    super.onDestroy()
    mOrientationEventListener!!.disable()
}

According to landscape/portrait mode you can rotate your views by setRotation property.

  • Related