Reputation: 4782
I have created a custom RadioButton for my Android app which just replaces the standard radio button with custom images. Now I want to have the text label that would usually appear to the right of the standard button appear overlapping the custom button in it's center.
Is there a way to do this?
UPDATE: Here is my attempt at create a custom component to do this:
public class RadioButtonText extends RadioButton {
Paint myPaint = new Paint();
public RadioButtonText(Context context) {
super(context);
}
public RadioButtonText(Context context, AttributeSet attrbs) {
super(context, attrbs);
}
@Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
String myText = (String) getText();
canvas.drawText(myText, 10, 10, myPaint);
}
}
And here is my using it in my layout.xml:
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="@drawable/bb_button"
android:id="@+id/levelButton0"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="1">
</view>
But when I try to run the app I get this error:
06-11 22:16:32.642: ERROR/AndroidRuntime(323): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.Activity.setContentView(Activity.java:1647)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:30)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): ... 11 more
06-11 22:16:32.642: ERROR/AndroidRuntime(323): Caused by: java.lang.NoSuchMethodException: RadioButtonText(Context,AttributeSet)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at java.lang.Class.getMatchingConstructor(Class.java:660)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at java.lang.Class.getConstructor(Class.java:477)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): ... 23 more
06-11 22:16:32.662: WARN/ActivityManager(42): Force finishing activity com.stickfigs.blockball/.BlockBallLevelSelect
06-11 22:16:33.198: WARN/ActivityManager(42): Activity pause timeout for HistoryRecord{43edc648 com.stickfigs.blockball/.BlockBallLevelSelect}
What am I doing wrong?
Upvotes: 4
Views: 2750
Reputation: 4218
The radioButton is a subclass of TextView, the image it's just the left drawable, so maybe it's easier to remove the drawable left and use the drawable bottom. The rest of styling is as any textview, so drawablePadding, paddings and margins applies.
<RadioButton
android:id="@+id/level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@null"
android:drawableBottom="@android:drawable/btn_radio"
android:text="1" />
Upvotes: 0
Reputation: 124632
RadioButtonText
is an inner class of BlockBallLevelSelect
. you cannot instantiate an instance of it without an existing outer class object. As such, you will need to mark RadioButtonText
as static
to reference the type in XML.
As a side note, since it will be static it no longer makes a lot of sense to leave it as an inner class.
Upvotes: 2