4

I have been trying to create a customized toggle button in native android, But I'm not able to achieve it. Here is the Image which I want to look like : enter image description here

can someone help me on this?

I'm Coder
  • 125
  • 2
  • 13

3 Answers3

3

If you need to create a custom switch, you don't have to create a custom view. In fact, with the help of some drawables you will be able to achieve what you want.

I will give an example and you can customize it to your needs.

There are three main resources that you need.

  1. Thumb
  2. Track
  3. Text Coloring.

Let's start with the Thumb which represents the moving part in the Switch.

thumb_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <corners android:radius="12dp" />
            <solid android:color="@android:color/white" />
            <size android:width="120dp" android:height="@dimen/toggle_button_height" />
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <corners android:radius="12dp" />
            <solid android:color="@android:color/white" />
            <size android:width="120dp" android:height="@dimen/toggle_button_height" />
        </shape>
    </item>
</selector>

Then we need to draw the track which represents the bottom layer of the Switch.

track_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <corners android:radius="12dp" />
            <solid android:color="@android:color/darker_gray" />
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <corners android:radius="12dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
</selector>

Now moving to the last part which is the text coloring to give our text a different color depending on the selection state.

text_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/darker_gray" android:state_checked="false" />
    <item android:color="@color/colorPrimary" android:state_checked="true" />
</selector>

We need to create a style for this text because it can't be added directly to the Switch. So, in your styles.xml create a new style for the text color.

styles.xml

<style name="SwitchTextAppearance" parent="@android:style/TextAppearance.Holo.Small">
    <item name="android:textColor">@drawable/text_color_selector</item>
    <item name="android:textSize">14sp</item>
</style>

Finally, we need to tie all parts together in our layout.

layout.xml

...
<androidx.appcompat.widget.SwitchCompat
    android:id="@+id/switch"
    android:layout_width="240dp"
    android:layout_height="40dp"
    android:layout_gravity="center"
    android:checked="false"
    android:textAllCaps="false"
    android:textOff="Off"
    android:textOn="On"
    android:textSize="15sp"
    android:thumb="@drawable/thumb_selector"
    app:showText="true"
    app:switchTextAppearance="@style/SwitchTextAppearance"
    app:track="@drawable/track_selector" />
...

Results

switch_off switch_on

Hamza Sharaf
  • 811
  • 9
  • 25
2

To achieve this you can use a TabLayout inside a MaterialCardView. The MaterialCardView is needed to draw the corner radius for the outer section. In the TabLayout you can use the app:tabBackground to set a Drawable Selector for the Tab selected/unselected state and the app:tabTextAppearance where you can set a custom font style or size for the Tab.

Xml Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:cardBackgroundColor="#7a87a4"
        app:cardCornerRadius="10dp">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:backgroundTint="@android:color/transparent"
            android:layout_margin="1dp"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabIndicatorColor="@android:color/transparent"
            app:tabIndicatorHeight="0dp"
            app:tabRippleColor="@android:color/transparent"
            app:tabTextColor="@android:color/black"
            app:tabSelectedTextColor="@android:color/black"
            app:tabBackground="@drawable/tabs_selector"
            app:tabTextAppearance="@style/CustomTabTextAppearance">

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="By Current Location" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="By Location Name" />

        </com.google.android.material.tabs.TabLayout>

    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>

where @drawable/tabs_selector is a drawable Selector to set the Tab Selected/Unselected state like below:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
            <corners android:radius="10dp" />
        </shape>
    </item>
    <item android:state_selected="false">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
            <corners android:radius="0dp" />
        </shape>
    </item>
</selector>

and @style/CustomTabTextAppearance is a Custom Tab Text Appearance in case you want to change the Tab size or Font Family like below:

<style name="CustomTabTextAppearance" parent="TextAppearance.MaterialComponents.Button">
    <item name="fontFamily">@font/roboto_mono</item>
    <item name="android:fontFamily">@font/roboto_mono</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textSize">14sp</item>
</style>

Result:

toggle_button

MariosP
  • 8,300
  • 1
  • 9
  • 30
0

Key classes are the following:

ToggleButton Switch SwitchCompat CompoundButton

You can even use Slider. Init the thumb and the track.

Krahmal
  • 195
  • 13
  • can you provide some sample for the same?? I didn't find anything matching my UI. – I'm Coder Dec 08 '21 at 07:01
  • You don' t need their UI. Just set them to transparent or gone. Write a grey rec shape xml as background, and a white shape as thumb. Draw the thumb and text in ```override fun onDraw(canvas: Canvas) {}```. – Krahmal Dec 08 '21 at 07:24
  • why do we have to use the onDraw function for this?? can we achieve this using any of the Default Material Design ? – I'm Coder Dec 08 '21 at 07:30
  • @Krahmal is here referring you to create a custom view, which you can only make by using `onDraw()`. However, for your usecase, you can make above example by using a few `drawables` and material `textviews`. – Praveen Dec 08 '21 at 07:39
  • Emmm. I misunderstood the “customized” you said. You only want a MD component with your attributes, right? – Krahmal Dec 08 '21 at 07:47
  • @Krahmal I also been looking for an implementation for Slider to make a similar custom view as vinayak, can you please provide an example or point to an article? Thank you! – Astrit Veliu Dec 08 '21 at 09:16
  • @Krahmal- yes exatly. – I'm Coder Dec 08 '21 at 09:33
  • @Praveen- can we create it using toggle Button and drawables? can you please provide a sample example for it? – I'm Coder Dec 08 '21 at 09:34