haha
haha

Reputation: 461

How to add tabs in material design actionbar

How to implement this at material design. I want to add tabs in actionbar.

material design is ...

navigation drawer area  | title area
                        | tabs area

I want to..

navigation drawer area  | tabs area

Upvotes: 3

Views: 371

Answers (1)

Collins Abitekaniza
Collins Abitekaniza

Reputation: 4588

Try to add the Tablayout inside the Toolbar,and set the supportActionBar as the 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));
    }

Upvotes: 1

Related Questions