Search code examples
androidandroid-layoutstylesandroid-tabhostandroid-style-tabhost

How to do Tab Host like Iphone in android?


I am newer in Android. I am doing android application. I have to do TabHost in android look like iPhone, I am sharing image. Please help me for layout.

I am trying so much but not get exactly solution.

enter image description here


Solution

  • You can apply the custom shape and selector for tab.

    shape_tab_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <!-- radius should be half of the desired TabLayout height -->
        <corners android:radius="15dp" />
        <solid android:color="@color/colorWhite"/>
    
    </shape>
    

    shape_tab_un_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <!-- color of the selected tab -->
        <solid android:color="@android:color/transparent" />
    
    
    </shape>
    

    selector_tab.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- drawable for selected tab -->
        <item
            android:drawable="@drawable/shape_tab_selected"
            android:state_selected="true"/>
    
        <!-- drawable for unselected tab -->
        <item
            android:drawable="@drawable/shape_tab_unselected"
            android:state_selected="false"/>
    
    </selector>
    

    Add tab layout in your activity layout and set selector_tab as tabBackground.

    <com.google.android.material.tabs.TabLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorGray"
    app:tabGravity="fill"
    app:tabBackground="@drawable/selector_tab"
    app:tabIndicatorColor="@android:color/transparent"
    app:tabIndicatorHeight="0dp"
    app:tabMode="fixed"
    app:tabRippleColor="@null"
    app:tabSelectedTextColor="@color/colorBlack"
    app:tabTextColor="@color/colorBlack"  />
    

    Customize other properties according to your need. You can find more about TabLayout here

    That's all.

    Happy Coding! :)