In the below example I have created a Tab layout using design support library.
When user clicks on tab, tab color will change, mean it will identify that you have clicked on that particular tab. Here, first I have added material design support library within Grandle file. After that In actvity_main.xml layout, I have added a toolbar and tab layout. Now see MainActivity, here I have created a tabinitialize()method, in this method I have initialized tablayout and set tabs text. Follow the below example code it clearly describes How to make Tab layout with using material design support library.
Step(1)-I have added first material design support library to Grandle file -
Introduction of library:- There is a design support library which provides us the facility to create dynamic tab layout in your application.
compile 'com.android.support:design:23.1.0'
Step(2)- activity_main.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weightSum="7"
android:background="#e7f1e7"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> <android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/Theme.AppCompat" /> </LinearLayout>
Step(3)-MainActivity-
private void tabinitialize() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("One"));
tabLayout.addTab(tabLayout.newTab().setText("Two"));
tabLayout.addTab(tabLayout.newTab().setText("Three"));
}
0 Comment(s)