5

good morning !

i would like to set an radio button right from the his text. i saw here an solution like this way:

 <RadioButton
            android:drawablePadding="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton"
            android:drawableRight="@android:drawable/btn_radio"
            android:button="@null"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:paddingStart="18dp"
            android:paddingEnd="12dp"
            android:textColor="@color/DefaultGrey"
            android:textSize="14sp"
            android:checked="false" />

this set the radio button right from his text, but i dislike the style of the alternative radio button, which i get with this code.

is there an way to use the default radio button style?

Sajib Acharya
  • 1,666
  • 5
  • 29
  • 54
Trombone0904
  • 4,132
  • 8
  • 51
  • 104

5 Answers5

3

Kotlin programmatic solution:

private fun createDrawableRightRadioButton(message: String): RadioButton {

    val radioButton = RadioButton(context)
    radioButton.layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)

    radioButton.setText(message)
    radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.resources.getDimension(R.dimen.text_medium)); //<dimen name="text_medium">14sp</dimen>
    radioButton.setTextColor(ContextCompat.getColorStateList(context, R.color.radio_grey_text_selector))
    radioButton.typeface = Typeface.create("roboto", Typeface.NORMAL)

    radioButton.layoutDirection = View.LAYOUT_DIRECTION_RTL
    radioButton.setButtonDrawable(0) // for removing default drawable
    radioButton.setCompoundDrawablePadding(10)
    radioButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.gostyle_radio_button_selector, 0); // for adding drawable on the right
    radioButton.gravity = Gravity.CENTER_VERTICAL

    return radioButton

  }
2

You just need to add the following code in Radio Button

   android:layoutDirection="rtl"

   eg.
   <RadioButton
         android:id="@+id/rb_male"
         android:layout_width="wrap_content"
         android:layoutDirection="rtl"
         android:layout_height="wrap_content"
         android:textColor="@color/text_color"
         android:text="Male" />
Sujeet
  • 558
  • 8
  • 13
1

Solved but next Problem

This was the solution:

<RadioGroup
    android:id="@+id/rgRight"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_below="@+id/HeaderSectionSort"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:weightSum="1">

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:button="@null"
        android:drawableRight="@drawable/ic_selected_sort"
        android:text="@string/Radio1"
        android:textColor="@color/DefaultGrey"
        android:textSize="14sp"
        android:height="40dp"
        android:paddingLeft="16dp"
        android:paddingRight="10dp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#f4f4f4"
        android:id="@+id/Seperator3"
        android:layout_marginTop="5dp" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:paddingLeft="16dp"
        android:button="@null"
        android:drawableRight="@drawable/ic_selected_sort"
        android:buttonTint="@color/DefaultGreen"
        android:text="@string/Radio2"
        android:textColor="@color/DefaultGrey"
        android:textSize="14sp"
        android:height="40dp"
        android:paddingRight="10dp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#f4f4f4"
        android:id="@+id/Seperator4"
        android:layout_marginTop="5dp" />

    <RadioButton
        android:id="@+id/radio3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:paddingLeft="16dp"
        android:button="@null"
        android:drawableRight="@drawable/ic_selected_sort"
        android:text="Radio3"
        android:textColor="@color/DefaultGrey"
        android:textSize="14sp"
        android:height="40dp"
        android:paddingRight="10dp" />


</RadioGroup>

Result:

enter image description here

Problem: i would like to hide the drawable (ic_selected_sort) at the beginning programmatically. and on click set it to visible.

i try something like this:

        rb1.setButtonDrawable(android.R.color.white);

but i doesn't take effect. any ideas? :)

Trombone0904
  • 4,132
  • 8
  • 51
  • 104
0

you can try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RadioButton
android:id="@+id/radiobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/radiobutton"
android:layout_alignBottom="@+id/radiobutton"
android:layout_alignParentLeft="true">
Saif
  • 723
  • 6
  • 21
  • there is the problem, that i can only touch the radio button to select it, not the text – Trombone0904 Dec 04 '15 at 07:07
  • you can add one layout and put both controller inside that layout (RadioButton,textview) and set click of that layout and handle Radio button select and unselect – Saif Dec 04 '15 at 07:16
  • you are use the ? – Saif Dec 04 '15 at 07:21
  • you are using RadioGroup then put android:layout_gravity="right" and each RadioButton to set android:gravity="right" – Saif Dec 04 '15 at 07:27
  • if we do this way, then we have to handle the clicks of textview also with more handling if we want to choose one. Using RadioGroup, we wont need to handle click events for the textview separately. – Akshay Kumar Both Jun 14 '19 at 06:25
0

This is what I've added to the Checkbox in my project:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true"
 android:drawable="@drawable/tick_green" >
    <shape >
        <size android:width="@dimen/d32"
            android:height="@dimen/d32"/>
    </shape>
</item>

 <item android:state_checked="false"
 android:drawable="@drawable/uncheck" >
    <shape >
        <size android:width="@dimen/d32"
            android:height="@dimen/d32"/>
    </shape>
    </item>

And my checkbox code looks like this:

<CheckBox
    android:id="@+id/reason_cb_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:button="@null"
    android:drawableRight="@drawable/my_checkbox_drawable"  <- set it here
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="@dimen/d16"
    android:paddingRight="@dimen/d16"
    android:text="@string/some_text" />

You can probably create a similar drawable and set it to your RadioButton drawableRight attribute.

Note: "uncheck" is a transparent drawable that I created for the unchecked state.

avin
  • 459
  • 5
  • 14