I want to customized Material Toggle Button like the following. I have tried but not succeed to achieve this output. following is xml code I tried but not desired output. I go through the Offical Documents but no help about this. Please help me if anyone knows about this Thanks
xml
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/outdoor_toggle_buttonGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:checkedButton="@+id/btn_walking"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_20sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<Button
android:id="@+id/btn_walking"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/walking"
android:textColor="@color/white"
android:textAllCaps="false"
android:backgroundTint="@color/green"
style="@style/Widget.MaterialComponents.Button"
app:shapeAppearance="@style/CustomShapeAppearance"
/>
<Button
android:id="@+id/btn_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/running"
android:textColor="@color/black"
android:textAllCaps="false"
android:backgroundTint="@color/white"
style="@style/Widget.MaterialComponents.Button"
app:shapeAppearance="@style/CustomShapeAppearance"
/>
<Button
android:id="@+id/btn_cycling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cycling"
android:textColor="@color/black"
android:textAllCaps="false"
android:backgroundTint="@color/white"
style="@style/Widget.MaterialComponents.Button"
app:shapeAppearance="@style/CustomShapeAppearance"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Style
<style name="CustomShapeAppearance">
<item name="cornerSizeTopLeft">20dp</item>
<item name="cornerSizeTopRight">20dp</item>
<item name="cornerSizeBottomLeft">20dp</item>
<item name="cornerSizeBottomRight">20dp</item>
</style>
output


This can be achieved using a
TabLayoutinside aMaterialCardView. TheMaterialCardViewis needed to draw the corner radius of the outer section and theTabLayoutto draw each Tab. TheTabLayouthas a propertyapp:tabBackgroundwhich can be used to set a Drawable selector for the Tab Selected/Unselected state.Xml layout:
where
@drawable/tabs_selectoris the Tab Drawable Selector to set the Selected/Unselected state like below:For
@drawable/tab_bg_selectedSelected state:For
@drawable/tab_bg_unselectedUnselected State:and
@style/CustomTabTextAppearanceis a custom Tab TextAppearance in case you want to change for each Tab the Text Size or Font Family like below:Result: