Reputation: 1269
I have created my radio buttons with text at left of its button in xml :
<RadioButton
android:id="@+id/radio_sattelite1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:button="@null"
android:checked="true"
android:drawableRight="@android:drawable/btn_radio"
android:gravity="center_vertical"
android:onClick="onRadioButtonClicked"
android:text="@string/sattelite1" />
For some reason I have to create some of them in my java code but I'm unable to design it as above.
EDIT: with the help of my friends @Hariharan and @Haresh I end up with this code :
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_radio);
drawable.setBounds(0, 0, 57, 72);
radioButton.setCompoundDrawables(null, null, drawable, null);
But now the problem is that both buttons at left and right appears.
Upvotes: 1
Views: 2752
Reputation: 24853
Try this..
yourradiobutton.setCompoundDrawables(drawable_left, drawable_top, drawable_right, drawable_bottom);
EDIT
RadioButton radioButton = new RadioButton(this);
radioButton.setText("sattelite1");
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_radio);
drawable.setBounds(0, 0, 57, 72);
radioButton.setCompoundDrawables(null, null, drawable, null);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setChecked(true);
radioButton.setButtonDrawable(android.R.color.transparent);
radioButton.setBackgroundDrawable(null);
Upvotes: 5
Reputation: 1269
Finally I get it to work with help of stackoverflow fellas :
radioButton.setGravity(Gravity.CENTER_VERTICAL);
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_radio);
drawable.setBounds(0, 0, 57, 72);
radioButton.setButtonDrawable(R.drawable.empty);
radioButton.setCompoundDrawables(null, null, drawable, null);
so what is the R.drawable.empty ? This is a png file without background that you have to create it.
Thanks ....
Upvotes: 0
Reputation: 502
Instead of adding text to radio button you can do one thing, in a linear layout with horizontal orientation, add one textview first and the next to that add radio button with no text as below:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" text="button text" >
</TextView>
<RadioButton
android:id="@+id/radio_sattelite1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="onRadioButtonClicked"/>
</LinearLayout>
Upvotes: 0
Reputation: 24848
Try this way,hope this will help you to solve your problem
RadioButton radioButton = new RadioButton(this);
radioButton.setText("sattelite1");
radioButton.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_launcher), null, null, null);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setChecked(true);
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Upvotes: 1