In Android studio we make an Android Tabs layout Example by using Android Design Support Library

Dependency: – Android Design Support Library

compile 'com.android.support:design:25.1.1'

First Make a Java File: – ActivityMain.java

package jeevan.karan.tabLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import jeevan.karan.R;
import jeevan.karan.tabLayout.adapter.FragmentAdapterClassChatingActivity;

public class ActivityOne extends AppCompatActivity {
TabLayout tabLayout ;
ViewPager viewPager ;
FragmentAdapterClassChatingActivity fragmentAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tab_layout1);
viewPager = (ViewPager) findViewById(R.id.pager1);
tabLayout.addTab(tabLayout.newTab().setText("Chat"));
tabLayout.addTab(tabLayout.newTab().setText("Contacts"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
fragmentAdapter = new FragmentAdapterClassChatingActivity(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(fragmentAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab LayoutTab) {
viewPager.setCurrentItem(LayoutTab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab LayoutTab) {
}
@Override
public void onTabReselected(TabLayout.Tab LayoutTab) {
}
});
}
}

Xml File: – activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.safal.sahilapp.tabLayout.ActivityOne">
<android.support.v4.view.ViewPager
android:id="@+id/pager1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout1"
style="@style/myTabView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabIndicatorColor="#aaafff"
app:tabMode="fixed"
app:tabSelectedTextColor="#aaaaaa"
app:tabTextColor="#5e5b5b" />
</LinearLayout>
</RelativeLayout>

Second Make Two Fragments: – ChattingFragment.java

package jeevan.karan.tabLayout.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import jeevan.karan.R;
/**
* Created by safal on 1/24/2017.
*/
public class ChattingFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_chating, container, false);
return view;
}
}

ContactFragment.java

package jeevan.karan.tabLayout.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import jeevan.karan.R;
/**
* Created by safal on 1/24/2017.
*/
public class ChattingFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_contact, container, false);
return view;
}
}

Also Make two Xml Files: – activity_chatting

<?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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20dp"
android:text="Chatting Fragment" />
</LinearLayout>

activity_contact.xml

<?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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20dp"
android:text="Contact Fragment" />
</LinearLayout>

Style: – In Vales/Styles add this code

<style name="myTabView" parent="Widget.Design.TabLayout">
<item name="tabIndicatorHeight">0.5dp</item>
<item name="tabBackground">@color/color1</item>
<item name="tabTextAppearance">@style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">10sp</item>
</style>

 

Third Make a Adapter: – FragmentAdapterClass.java

package jeevan.karn.tabLayout.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import jeevan.karn.tabLayout.fragment.ChattingFragment;
import jeevan.karn.tabLayout.fragment.FragmentContact;
/**
* Created by safal on 1/6/2017.
*/
public class FragmentAdapterClassChatingActivity extends FragmentStatePagerAdapter {
int TabCount;
public FragmentAdapterClassChatingActivity(FragmentManager fragmentManager, int CountTabs) {
super(fragmentManager);
this.TabCount = CountTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
ChattingFragment tab1 = new ChattingFragment();
return tab1;
case 1:
FragmentContact tab2 = new FragmentContact();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return TabCount;
}
}