Search code examples
javaandroidandroid-layoutscreen-orientationandroid-orientation

android Change orientation removing button


I've set android:configChanges="orientation|screenSize" on an activity to stop it restarting the activity everytime the orientation changes. Now this works fine but I think it's stopping the correct layouts from being used.

E.G. I have different layout folders for different orientations and sizes of screen. So if I start the activity in portrait, when I change the orientation to landscape, it's not using my landscape layout.

Also if I start the activity in landscape, when I change the orientation to portrait, it's not using my portrait layout.

Basically what I want the app to do is not start the activity again once the orientation changes, but use the correct layout when the orientation is changed!

I was thinking I could use the onConfigurationChanged method to explicitly change the layout in code?

Thanks for any input


Solution

  • So What I've ended up doing is keeping android:configChanges="orientation|screenSize" in the manifest file. Doing it this way stops me being able to use my correct layout folders for portrait and landscape.

    To overcome this issue, I override the onConfigurationChange method to set the correct information I needed for the activity to run as expected once the orientation changes. below is the code I've used:

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.hearing_test);
        //Any other information needed for the activity to work correctly
    }
    

    Thanks for the help and guidance @mihail, helped me get to the bottom of the issue.