Goofy
Goofy

Reputation: 6128

Programatically remove radio button and display only text

I have a radio button with text displaying like this:

0 About ,here 0 indicates that its a radio button, i want to progrmatically remove radio button and display only text "About" how to do this?

I have tried :

radioButton.setButtonDrawable(android.R.color.transparent);// only hides the radio button
radioButton.setButtonDrawable(android.R.empty);// not working

Thanks in advance.

Upvotes: 1

Views: 2965

Answers (4)

moh.sukhni
moh.sukhni

Reputation: 2230

  • Have you tries the following line to the hide the button drawable:

    radioButton.setButtonDrawable(new ColorDrawable(0xFFFFFF));

this will hide the drawable on the left side.

Upvotes: 1

vilpe89
vilpe89

Reputation: 4702

Just dont set text at all to RadioButton and use separate TextView as its text.

Then when you want to hide RadioButton just use setVisibility(View.GONE); (View.GONE will hide the whole thing, no space at all there) and to show it again setVisibility(View.VISIBLE);

Here's a little example with layout of how to use it:

XML

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio button text" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toggle visibility" />

</LinearLayout>

Code

final RadioButton radioButton = (RadioButton) findViewById(R.id.radioButton1);
findViewById(R.id.toggleButton1).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if(radioButton.getVisibility() == View.VISIBLE) {
            radioButton.setVisibility(View.GONE);
        }
        else if(radioButton.getVisibility() == View.GONE) {
            radioButton.setVisibility(View.VISIBLE);
        }
    }
});

Upvotes: 0

Dyna
Dyna

Reputation: 2305

xml layout:

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ButtonText" />

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

To hide radiobutton:

    radioButton.setVisibility(View.GONE);

Upvotes: 0

Aman
Aman

Reputation: 220

You can setText at runtime using setText() method

radioButton.setText("About");

May be you want this.

Upvotes: 0

Related Questions