Enable display orientation change when certain fragments are visible and disable when others

I use fragments in my app. Most of the fragments should have only one orientation – portait. So I forced holder activity to always be at portrait mode.

But one of my fragments should have both portrait and landscape modes. How to enable orientation change for only one fragment? Or I need to make it a separate activity to handle orientation without depending on other fragments?

Answer

You will have to do it manually.

1) add this line to your activity tag android:configChanges=”orientation|screenSize”

2) Override config change callback in you fragmtn or activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig); 

   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();
    }

//Your fragment animation layout changing code

}

Leave a Reply

Your email address will not be published. Required fields are marked *