The xml is not switching when device orientation change The xml is not switching when device orientation change android android

The xml is not switching when device orientation change


The manifest code

android:configChanges="orientation|screenSize"

ignores the XML in "layout-land" and uses the one in the "layout" folder. If you create a different XML for landscape don't use the android:configChanges="orientation|screenSize" tag for that activity.


android:configChanges="orientation" stops the activity from restarting, so also from reloading the xml layout (you normally do this in onCreate).Instead, onConfigurationChanged(newConfig) is called. So you can do:

@Override    public void onConfigurationChanged(Configuration newConfig){        super.onConfigurationChanged(newConfig);        setContentView(R.layout.<xml file>);    }

This wil reload the layout from the layout-land dir, if available.Note: you will also need to link actions to buttons and things like that


private void setContentBasedOnLayout(){    WindowManager winMan = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);    if (winMan != null)    {        int orientation = winMan.getDefaultDisplay().getOrientation();        if (orientation == 0) {            // Portrait            setContentView(R.layout.alertdialogportrait);        }        else if (orientation == 1) {            // Landscape            setContentView(R.layout.alertdialoglandscape);        }                }}