Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically set android:button="@null"?

Tags:

I'm trying to programmatically add a set of RadioButtons to a RadioGroup like so:

for (int i = 0; i < RANGE; i++) {     RadioButton button = new RadioButton(this);     button.setId(i);     button.setText(Integer.toString(i));     button.setChecked(i == currentHours); // Select button with same index as currently selected number of hours     button.setButtonDrawable(Color.TRANSPARENT);     button.setBackgroundResource(R.drawable.selector);     button.setOnClickListener(new OnClickListener() {         @Override         public void onClick(View view) {             ((RadioGroup)view.getParent()).check(view.getId());             currentHours = view.getId();         }     });      radioGroupChild.addView(button); } 

and I need to set the button drawable to null (I want just text on top of my background). Manually doing this in the XML with android:button="@null" works great, but I don't want to hardcode each radio button. I've tried just doing button.setButtonDrawable(null) but it doesn't change anything.

Any help is greatly appreciated!

like image 870
Cornholio Avatar asked Apr 29 '13 17:04

Cornholio


2 Answers

You need to set an empty StateListDrawable as the drawable. So the Java equivalent of android:button="@null" is:

radioButton.setButtonDrawable(new StateListDrawable()); 
like image 174
georgiecasey Avatar answered Sep 24 '22 18:09

georgiecasey


You should do this:

button.setBackgroundDrawable(null); 

If your drawable has a reference to selector you can make it transparent through your selector xml:

<item android:drawable="@android:color/transparent" /> 

as you'd probably found the solution ;)

like image 41
Neoh Avatar answered Sep 24 '22 18:09

Neoh