Thursday, May 28, 2015

Check if we are in Dual Pane

This is a common problem and with a variety of solutions. In the solution I'll show you in a little while we check in our Activity if there is an id for the 2nd Fragment ( the one that will play the role of the DetailFragment ).

One common tactic is having two different versions of the xml layout we are wokring on. One in the layout folder and another one in the layout-sw600dp or whatever we define for the dual mode.

The 2nd file contains one more FrameLayout so what we basically have to do is check if the view is there. If its true then we are on a dual mode, if not then we are on a single mode.

First we define a Boolean isDualMode with false as an initial value.
Then on onCreate we check for the details id :  

View aView = findViewById(;
if (aView != null) {
    isDualPane = true;

If thats true we add and commit the DetailsFragment also.

Full code for onCreate() :

    protected void onCreate(Bundle savedInstanceState) {

        View aView = findViewById(;
        if (aView != null) {
            isDualPane = true;
        } fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction();

        mainFragment = new MainFragment();

        if (isDualPane) {
            detailsFragment = new DetailsFragment();
            fragmentManager.beginTransaction().add(, detailsFragment).commit();

        fragmentManager.beginTransaction().add(, mainFragment).commit();


No comments:

Post a Comment