Search code examples
androidandroid-actionbarandroid-orientation

How to remove ActionBar tabs in landscape orientation only for tablets


I'm currently writing an app that uses ActionBar tabs for navigation. When the device is in portrait mode, there are two tabs the user can select from, each with an associated fragment. When the tablet switches to landscape, I would like these tabs to disappear because I have a two pane layout defined for landscape orientation where each pane hosts one of the fragments. I know during switches the Activity is recreated, so I can achieve this in code by only creating tabs in my OnCreate when the orientation is portrait. However, the two pane layout is only used for tablets (I have it defined in layout-large-land). For phones in landscape, the one pane layout with tabs should be used. Is there a way I can code in something like "Only use tabs when the two pane layout file is being inflated" i.e. tabs should only appear if the device is a tablet and in landscape mode.


Solution

  • Add boolean variable to a the resources inside values-large-land and set it to true, then in code use it to setup the tabs:

    boolean isDualPane = getResources().getBoolean(R.boolean.dualPane);
    if(!isDualPane) {
     setupActionBarTabs();
    }