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(R.id.details);
if (aView != null) {
    isDualPane = true;
}

If thats true we add and commit the DetailsFragment also.

Full code for onCreate() :

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        View aView = findViewById(R.id.details);
        if (aView != null) {
            isDualPane = true;
        }

        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        mainFragment = new MainFragment();

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

        fragmentManager.beginTransaction().add(R.id.main, mainFragment).commit();
        fragmentTransaction.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 :
    getSupportActionBar().setDisplayShowTitleEnabled(false);

2) Add TextView in Toolbar :

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:minHeight="?android:attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        android:background="@color/colorPrimary"
        android:theme="@style/ActionBarThemeOverlay"
       android:popupTheme="@style/ActionBarPopupThemeOverlay">

    <TextView
        android:id="@+id/tv_toolbar"
        android:textSize="@dimen/toolbar_textSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</android.support.v7.widget.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.