3

I want to set the visibility of a RadioButton to INVISIBLE or GONE. For some reason this isn't working.

RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id);
myRadioButton.setVisibility(View.GONE);

or

myRadioButton.setVisibility(View.INVISIBLE);

No error is returned, it just doesn't do anything.

However I have tried this on the RadioGroup

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers);
myRadioGroup.setVisibility(View.INVISIBLE);

And it works fine to hide the whole group. Is there a way to just hide one of the RadioButtons? I have a group of 3 which are answers to questions but in some cases I want to hide the last one.

LondonAppDev
  • 8,501
  • 8
  • 60
  • 87

3 Answers3

7

you can hide the particular radio button this way

RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.INVISIBLE);

or if you use View Gone menas radio button hide with sapce

RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.GONE);

In that case do not hide the radio group

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers);
myRadioGroup.setVisibility(View.Visible);
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
1

Hi use like this.

RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id);
myRadioButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            myRadioButton.setVisibility(View.INVISIBLE);
        }
    });

OR

  <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" 
        android:visibility="invisible"/>

This code is working for me.Hope this will help you.

Nirmal
  • 2,340
  • 21
  • 43
  • This does work, however in the format above (where I programmatically hide it in the onCreate method) it doesn't. Unfortunately making it hide when it's clicked doesn't help me in this situation. – LondonAppDev Oct 01 '13 at 05:48
  • @MarkWinterbottom see my edited answer you can do it in XML itself. – Nirmal Oct 01 '13 at 05:52
0
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TableLayout bilgiAlani=(TableLayout)findViewById(R.id.bilgiAlani);
    final RadioButton secim1 = (RadioButton) findViewById(R.id.butonSecim1);
    final TextView bilgiMesaji=(TextView)findViewById(R.id.bilgiMesaji);
    secim1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (secim1.isChecked()) {
                Toast.makeText(getApplicationContext(), "secildi",
                        Toast.LENGTH_SHORT).show();
                bilgiAlani.setVisibility(View.VISIBLE);
                bilgiMesaji.setText("birinci seicmbirinci seicmbirinci seicmbirinci seicmbirinci\n seicmbirinci seicm" +
                        "birinci seicmbirinci seicmbirinci seicmbirinci seicm" +
                        "birinci seicmbirinci seicm ");
            }
            else if(!secim1.isChecked()) 
            {
                Toast.makeText(getApplicationContext(), "Secmekten Vazgecildi",
                        Toast.LENGTH_SHORT).show();
                bilgiAlani.setVisibility(View.GONE);
                bilgiMesaji.setText("birinci secilmedi");
            }

        }
    });