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


Thursday, May 21, 2015

Set text size for Toolbar

Since Toolbar doesnt provide a textSize attribute to set the text size of the Title you have to add a TextView and pass the title text to it.
Ofcourse you can instead use setTitleTextAppearance where you can set color, size, style etc.

For the first solution the steps you got to follow are :

1) remove default title :

2) Add TextView in Toolbar :




3) Set toolbar_textSize on dimens.xml :

<dimen name="toolbar_textSize">16sp</dimen>

Now toolbar's title size is the one you specify on the dimens.xml  file. That's it 16sp in our code.
We prefer sp units (instead of dp) so the size can scale when user changes the font size on her/his device.