Reputation: 4268
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?
Upvotes: 5
Views: 8632
Reputation: 576
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" />
Upvotes: 2
Reputation: 382
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
}
Upvotes: 3
Reputation: 469
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.
Upvotes: 0
Reputation: 4268
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:
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? :)
Upvotes: 1
Reputation: 713
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">
Upvotes: 0