Question

I tried to do android:drawableLeft and android:drawableRight for Switch

drawableLeft is showing fine but drawableRight is showing behind the track of the Switch

how to make the track to display in center?

My Code:

<Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/moon"
        android:drawablePadding="10dp"
        android:drawableRight="@drawable/sun"
        android:textOff=""
        android:textOn=""
         />

I am getting like this

switch Off

switch Off

Switch On

Switch On

Was it helpful?

Solution

I achieved it by adding 2 extra widgets like Linearlayout and ImageView

<LinearLayout
                style="@style/Wrap"
                android:layout_height="match_parent"
                android:layout_gravity="bottom"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:gravity="center" >

                <Switch
                    android:id="@+id/Sw_am_pm_button"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:checked="true"
                    android:drawableLeft="@drawable/moon"
                    android:drawablePadding="6dp"
                    android:switchMinWidth="40dp"
                    android:textOff=""
                    android:textOn=""
                    android:thumb="@drawable/circle"
                    android:track="@drawable/bg" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/sun" />
            </LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top