Skip to content

Instantly share code, notes, and snippets.

@siyamed
Created November 22, 2014 01:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save siyamed/7278413851d3a1d02ffc to your computer and use it in GitHub Desktop.
Save siyamed/7278413851d3a1d02ffc to your computer and use it in GitHub Desktop.
Android RadioButton With Text Drawable Aligned Centered With Text
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ImageRadioButton">
<attr name="radioTextImage" format="reference" />
</declare-styleable>
</resources>
/**
*
* Android radio button that draws (state list) drawable to immediate left of its text.
* RadioButton has an attribute drawableLeft or drawableStart, however those
* draw the drawable to the left of the component and does not center align
* with the text itself.
*
* This ui component also supports statelistdrawables and state changes on
* the drawable.
*
* It is possible to create a custom layout for such cases, but then it becomes
* cumbersome to use RadioGroup since it has instance checks for RadioButton
*
* &lt;com.github.siyamed.gist.ImageRadioButton
* android:layout_width=&quot;match_parent&quot;
* android:layout_height=&quot;wrap_content&quot;
* android:gravity=&quot;center&quot;
* android:button=&quot;@null&quot;
* android:text=&quot;RadioText&quot;
* app:radioTextImage=&quot;@drawable/radio_selector&quot;/&gt;
*
* where radio_selector is a regular state drawable:
*
* &lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
* &lt;item android:state_checked=&quot;true&quot; android:drawable=&quot;@drawable/my_drawable_selected&quot; /&gt;
* &lt;item android:state_pressed=&quot;true&quot; android:drawable=&quot;@drawable/my_drawable_selected&quot; /&gt;
* &lt;item android:drawable=&quot;@drawable/my_drawable&quot; /&gt;
* &lt;/selector&gt;
*
*/
package com.github.siyamed.gist;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.RadioButton;
public class ImageRadioButton extends RadioButton {
private ImageSpan imageSpan;
private Drawable drawable;
public ImageRadioButton(Context context) {
super(context);
init(context, null, -1);
}
public ImageRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, -1);
}
public ImageRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ImageRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr);
}
private final void init(Context context, AttributeSet attrs, int defStyleAttr) {
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageRadioButton, defStyleAttr, 0);
drawable = typedArray.getDrawable(R.styleable.ImageRadioButton_radioTextImage);
typedArray.recycle();
}
if(drawable != null) {
imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
setText(getText());
}
}
@Override
public void setText(CharSequence text, BufferType type) {
Spannable newText = new SpannableString(" " + text);
newText.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
super.setText(newText, type);
}
@Override
public void jumpDrawablesToCurrentState() {
super.jumpDrawablesToCurrentState();
if(drawable != null) {
drawable.jumpToCurrentState();
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if(drawable != null) {
drawable.setState(getDrawableState());
}
}
@Override
protected boolean verifyDrawable(Drawable who) {
if(drawable != null) {
return super.verifyDrawable(who) || who == drawable;
}
return super.verifyDrawable(who);
}
}
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/my_drawable_selected" />
<item android:state_pressed="true" android:drawable="@drawable/my_drawable_selected" />
<item android:drawable="@drawable/my_drawable" />
</selector>
<com.github.siyamed.gist.ImageRadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:button="@null"
android:text="RadioText"
app:radioTextImage="@drawable/radio_selector"/>
@theGreatHeisenberg
Copy link

how do I set radioTextImage via java?

@jackyhieu1211
Copy link

It not working android 7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment