0

I am using some customized RadioButtons in Android. Unfortunately the text can't be aligned in the center altough I set the gravity and textAlignment attributes to "center". You can see in my screenshot that the text is more on the right side:

enter image description here

Here you see the code: Radio Button in the constraintLayout:

  <RadioButton
        android:id="@+id/r_Button_Small"
        android:layout_width="@dimen/_73sdp"
        android:layout_height="@dimen/_28sdp"
        android:layout_weight="1"
        android:textSize="@dimen/_12ssp"
        android:background="@drawable/background_selector"
        android:text="@string/small"
        android:textColor="@drawable/text_selector"
        android:gravity="center"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.322"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="@dimen/_50sdp"
        app:layout_constraintVertical_bias="0.584"
         />

Background files

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

    <item android:drawable="@drawable/state_unchecked" />

</selector>

with state file "checked"

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorBlue">
    </solid>
    <corners android:radius="@dimen/_25sdp"></corners>
</shape>

and "state uncheched"

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorTest">
    </solid>
    <corners android:radius="@dimen/_25sdp"></corners>
</shape>

If you are wondering what "@dimen/_25sdp" is: It is scalable size unit from https://github.com/intuit/sdp

Do you know how I can align the text into the center? I'appreciate every comment and would be thankful for your help.

VanessaF
  • 515
  • 11
  • 36
  • check out the attribute `android:drawablePadding=""`. I think that might help. – Suvajit Patra Jan 02 '21 at 09:53
  • Watch [this link](https://stackoverflow.com/questions/2134591/add-margin-between-a-radiobutton-and-its-label-in-android) for full explanation. – Suvajit Patra Jan 02 '21 at 09:59
  • Thanks Suvajit for your answer. Which value do I have to specify for that attribute? – VanessaF Jan 02 '21 at 09:59
  • just hardcode it and check what fits for you. Give them in `sp`s. – Suvajit Patra Jan 02 '21 at 10:10
  • Another thing you can do which is little bit messy. You can make a `LinearLayout`, which has 2 children one is a `RadioButton` without any label and another is a `TextView`. Now the `TextView` can be in the middle. – Suvajit Patra Jan 02 '21 at 10:18
  • Basically you have add paddingEnd to `RadioButton` exactly to the size of circle . I think a better workaround for it to create a Custom `ViewGroup` and make it act like `RadioButton`just mentioned in comment above. This will work if There is no `RadioGroup` . – ADM Jan 02 '21 at 13:50
  • Thanks for your answers. I will try to use the hard coding approach – VanessaF Jan 03 '21 at 16:19

1 Answers1

0

change your gravity with center to fill and remove text alignment

android:gravity="fill"
  • Thanks for your answer Tushar. If I use your solutions the vertical alignment gets incorrect. The text is then not aligned to the circle of the RadioButton which does not look good – VanessaF Jan 02 '21 at 10:10