How to reduce material.switchmaterial.SwitchMaterial end margin in android?

1.1k views Asked by At

I've used "com.google.android.material.switchmaterial.SwitchMaterial" in that left margin having some space. How to reduce that?

I've need align straight of the everyone text. I'm not add end margin or padding of the switch, Parent layout margin only I've set. Below my XML code and styles. Please provide your suggestion to reduce that margin, Thanks.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">

    <RelativeLayout
        android:id="@+id/who_can"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <com.google.android.material.textview.MaterialTextView
            style="@style/TextBlackRegular"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_toStartOf="@+id/video_view_result"
            android:singleLine="true"
            android:text="@string/who_can_view"
            android:textSize="@dimen/text_small"
            android:visibility="visible" />

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/video_view_result"
            style="@style/TextBlackRegular"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:singleLine="true"
            android:text="Everyone"
            android:textSize="@dimen/text_small" />

    </RelativeLayout>


    <com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/save_switch"
        style="@style/SwitchButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/who_can"
        android:checked="true"
        android:text="@string/save_gallery"
        android:textSize="@dimen/text_small"
        app:trackTint="@color/colorSecondaryText" />
</RelativeLayout>


    <style name="SwitchButtonStyle" parent="Widget.MaterialComponents.CompoundButton.Switch">
            <item name="thumbTint">@drawable/switch_selector</item>
            <item name="android:fontFamily">@font/font_regular</item>
            <item name="android:textColor">@color/colorBlack</item>
        </style>

enter image description here

4

There are 4 answers

2
AudioBubble On

In your relativeLayout/topRelativeLayout, You added margin. Which means you want to margin from every side. That's why It's happening

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">

remove the android:layout_margin="10dp". Add margin_top, margin_right what you wanna do.

0
btm me On

You should give wrap content for switch width , or make it inside a new Relative layout

0
pankaj jha On

if your end margin is showing after removing from

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">

then

<com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/save_switch"
        style="@style/SwitchButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/who_can"
        android:checked="true"
        android:text="@string/save_gallery"
        android:textSize="@dimen/text_small"
        app:trackTint="@color/colorSecondaryText" />

change in SwitchButtonStyle it gives you some default value we are happy if you share whole file

0
NataTse On

you can see that default drawables (thumb and track) from a theme have this paddings

enter image description here

so the way is try to use your custom drawables (but this may affect their appearance)

        app:track="@drawable/track"
        android:thumb="@drawable/thumb"