samjesse
samjesse

Reputation: 378

Android EditText Visibility based on radioGroup selection

I need to hide editText1 and show editText2 if radioButton1 is selected and I need to hide editText2 and show editText1 if radioButton2 is selected. both buttons are in a radioGroup. I am not sure if there is a radioGroup onChange event which returns which radioButton is checked? if so, then I can do

EditText et1 = (EditText) findViewById(R.id.editText1);
et1.setVisibility(View.INVISIBLE);

Upvotes: 2

Views: 3478

Answers (3)

A.R.
A.R.

Reputation: 2641

You can achieve your functionality as answered by Rami and sv3k.

There is another option that you can try to achieve what you want. That is setting onClickListener on each Radio Button.

Like this.

   RadioGroup  radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
        RadioButton radio1= (RadioButton)radioGroup.findViewById(R.id.radio1);
        RadioButton radio2= (RadioButton)radioGroup.findViewById(R.id.radio2);
        radio1.setOnClickListener(new View.OnClickListener() 
         {
             @Override
             public void onClick(View v) {
                 // TODO Auto-generated method stub
                 et1.setVisibility(View.INVISIBLE);
                 et2.setVisibility(View.VISIBLE);
             }
         });

        radio2.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                et2.setVisibility(View.INVISIBLE);
                et1.setVisibility(View.VISIBLE);
            }
        });

Upvotes: 0

sv3k
sv3k

Reputation: 11

You need to add OnCheckedChangeListener on your radioGroup:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {         
        // checkedId is the RadioButton selected
        if (checkedId == R.id.radioButton1) {
            editText1.setVisibility(View.INVISIBLE);
            editText2.setVisibility(View.VISIBLE);
        } else if (checkedId == R.id.radioButton1) {
            editText1.setVisibility(View.VISIBLE);
            editText2.setVisibility(View.INVISIBLE);
        }
    }
});

Upvotes: 0

Rami
Rami

Reputation: 7929

You can set OnCheckedChangeListener() to your radioGroup:

      final EditText et1 = (EditText) findViewById(R.id.editText1);
      final EditText et2 = (EditText) findViewById(R.id.editText2);

      RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
      radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId == R.id.radioBtn1){
                       et1.setVisibility(View.VISIBLE);
                       et2.setVisibility(View.INVISIBLE);  
                    } else {
                       et1.setVisibility(View.INVISIBLE);
                       et2.setVisibility(View.VISIBLE);  
                    }
                }
       });

Upvotes: 3

Related Questions