How to add tabs to the Material Design action bar
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 to share