rsanath
rsanath

Reputation: 1210

Why isn't onConfigurationChanged being called?

I'm writing a code that changes the TextView's text when the orientation is changed I am using the onConfigurationChanged() listener for changing the text

override fun onConfigurationChanged(newConfig: Configuration?) {
    super.onConfigurationChanged(newConfig)

    val orientation : Int = getResources().getConfiguration().orientation
    val oTag = "Orientation Change"

    if(orientation == (Configuration.ORIENTATION_LANDSCAPE)){
        mytext?.setText("Changed to Landscape")
        Log.i(oTag, "Orientation Changed to Landscape")

    }else if (orientation == (Configuration.ORIENTATION_PORTRAIT)){
        mytext?.setText("Changed to Portratit")
        Log.i(oTag, "Orientation Changed to Portratit")
    }else{
        Log.i(oTag, "Nothing is coming!!")
    }
}

but nothing happens not even in the Log

also I have added this attribute to my Activity in manifest file

android:configChanges="orientation"

What am I missing here ? also is there any other way to achieve this?

Upvotes: 3

Views: 6892

Answers (1)

Nithinlal
Nithinlal

Reputation: 5061

Use this code to get the confi chage

override fun onConfigurationChanged(newConfig: Configuration?) {
        super.onConfigurationChanged(newConfig)
        if (newConfig != null) {
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
            }
        }
    }

A couple of things to try:

android:configChanges="orientation|keyboardHidden|screenSize" rather than android:configChanges="orientation"

Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); anywhere. This will cause onConfigurationChange() to not fire.

Upvotes: 6

Related Questions