0

Good day,

I am having difficulties to adjust the switch compat to the corresponding length. I´ll explain, I want to get to this

enter image description here

enter image description here

The "términos de uso" (iv_back) and the uso datos móviles (witch_mobile_data) are an item (part of a recycler) that are decided at run time (depending on logic, one is painted or the other). "términos de uso" has a imageview with the arrow (iv_back). I want the same length for the uso datos móviles as the image above shows.

Right now I am getting this, you can see the tracks size is bigger that what I want above. I have tried everything, maybe I am complicating this, so there has to be an easier way to achieve that.

enter image description here

Here is my code:

This is part of the item of the recycler_view

 ...
        <FrameLayout
            android:id="@+id/action_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/size_40"
            app:layout_constraintBottom_toBottomOf="@id/tv_configuration_title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/tv_configuration_title">

            <ImageView
                android:id="@+id/iv_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_configuration_arrow" />

            <androidx.appcompat.widget.SwitchCompat
                android:id="@+id/switch_mobile_data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:thumb="@drawable/thumb_layerlist"
                app:track="@drawable/track_backgrounds"
                app:showText="false" />

        </FrameLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

track_backgrounds.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/neutral_20" />
            <corners android:radius="100dp" />
            <size android:height="32dp" android:width="44dp" />
        </shape>
    </item>

    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="@color/neutral_20" />
            <corners android:radius="100dp" />
            <size android:height="32dp" android:width="44dp" />
        </shape>
    </item>
</selector>

thumb_layerlist.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:gravity="fill"
        android:drawable="@drawable/thumb_backgrounds"/>

    <item
        android:gravity="center"
        android:drawable="@drawable/thumb_icons"
        android:top="1dp"
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"/>

</layer-list>

thumb_icons.xml

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

thumb_backgrounds.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape android:shape="oval">
            <solid android:color="@color/white" />
            <size android:width="20dp" android:height="20dp" />
        </shape>
    </item>

    <item android:state_checked="false">
        <shape android:shape="oval">
            <solid android:color="@color/neutral_50" />
            <size android:width="20dp" android:height="20dp" />
        </shape>
    </item>

</selector>
JaviMar
  • 375
  • 5
  • 18

0 Answers0