How to add tabs to the Material Design action bar

How to implement this when designing materials. I want to add tabs to the action bar.

material design ...

navigation drawer area | title area
                        | tabs area

I want to..

navigation drawer area | tabs area
+3


source to share


1 answer


Try to add Tablayout to toolbar and set ActionBar support as toolbar in your Java code.

Example

Android XML



<android.support.v7.widget.Toolbar android:layout_width="match_parent"
                                               android:layout_height="?attr/actionBarSize"
                                               android:id="@+id/toolbar"
                                               android:background="?attr/colorPrimary">

                <android.support.design.widget.TabLayout
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:id="@+id/tabLayout"/>
            </android.support.v7.widget.Toolbar>

      

Java code

Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

 String[] tabs={
            "Tab 1","Tab 2","Tab 3"
    };

    TabLayout tabLayout=(TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    for(String name:tabs){
        tabLayout.addTab(tabLayout.newTab().setText(name));
    }

      

+1


source







All Articles