Search code examples
androidscreen-orientation

How to detect screen orientation in a master/detail flow in android


I'm using the master/detail flow of android and I'm trying to detect the screen orientation in order to make the master part smaller in portrait mode but I'm having some difficulties, I wrote this method :

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

            setContentView(R.layout.activity_analyse_list);
            LinearLayout layout = (LinearLayout) findViewById(R.id.mastermenu);

            layout.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, (float) 0.5));
        }
    }

And i put it in the ListActivity which is the first and main activity but nothing happen when I flip the tablet, I tried to use Log.d to see if the onConfigurationChanged is called on the rotation but nothing on the logcat.


Solution

  • If you want 2 different Layouts depending on Screen orientation, you can define a different layout for landscape by adding a layout-land folder and defining ad different layout for that orientation.

    In that layout you can make any changes you like, compared to portrait. You dont have to do anything yourself, android will take care of it