1. Master layout file

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   tools:context="com.bxkj.dylan.tablayoutreddot.MainActivity">
   <android.support.design.widget.TabLayout
       android:id="@+id/tabLayout"
       app:tabBackground="@android:color/white"
       app:tabTextColor="@color/colorBlack"
       app:tabSelectedTextColor="@color/colorAccent"
       app:tabMode="scrollable"
       android:layout_width="match_parent"
       android:layout_height="40dp" />
</LinearLayout>

Copy the code

2. Custom layout file to display red dot

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_gravity="center"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tv_tab_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/colorBlack"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/iv_tab_red"
        android:layout_gravity="right"
        android:layout_width="18dp"
        android:text="5"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:layout_height="18dp"
        android:background="@drawable/red_dot" />
</LinearLayout>
Copy the code

3. Set each Tab loaded by TabLayout


import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/ * * *@author dylan
 */
public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private TextView tv_tab_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = findViewById(R.id.tabLayout);
        initData();
    }

    private void initData(a) {
        TabLayout.Tab tab = tabLayout.newTab().setText("All");
        tabLayout.addTab(tab);

        // Waiting for payment column - loads a custom layout showing little red dots
        tab = tabLayout.newTab();
        tab.setCustomView(R.layout.tab_wait_for_pay);
        tv_tab_title = tab.getCustomView().findViewById(R.id.tv_tab_title);
        tv_tab_title.setText("To be paid");
        tabLayout.addTab(tab);


        tab = tabLayout.newTab().setText("To be shipped");
        tabLayout.addTab(tab);

        tab = tabLayout.newTab().setText("To be received");
        tabLayout.addTab(tab);

        tab = tabLayout.newTab().setText("Done");
        tabLayout.addTab(tab);

        tab = tabLayout.newTab().setText("Cancelled");
        tabLayout.addTab(tab);

        // Add a tabLayout check listener
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // Set the text color when selected
                if(tab.getCustomView() ! =null) { tv_tab_title.setTextColor(getResources().getColor(R.color.colorAccent)); }}@Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // Sets the color of the text when it is not selected
                if(tab.getCustomView() ! =null) { tv_tab_title.setTextColor(getResources().getColor(R.color.colorBlack)); }}@Override
            public void onTabReselected(TabLayout.Tab tab) {}}); }}Copy the code

4.GitHub