Tamer
Tamer

Reputation: 111

enable / disable edittext using radio button in android

I am new in android. I have two radio buttons and edit text.to check the radio button, it disable the edit text and prevent input pad from appearing and to check the radio button2 enable edit text... I tried a lot of answer but nothing works. this is my code

public  class MainActivity extends AppCompatActivity {

    RadioButton radioButton, radioButton2;
    EditText editText;
    RadioGroup radioGroup;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioButton = (RadioButton) findViewById(R.id.radioButton);
        radioButton.setChecked(true);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        if ((radioButton.isChecked())) {
            editText.setEnabled(false);
            editText.setInputType(InputType.TYPE_NULL);
            editText.setFocusableInTouchMode(false);
        } 
        else {
            editText.setEnabled(false);
            editText.setFocusableInTouchMode(false);
        }
    }
}

Upvotes: 0

Views: 3079

Answers (4)

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch(checkedId)
        {
            case R.id.radio0:
                editText.setEnabled(false);
                editText.setInputType(InputType.TYPE_NULL);
                editText.setFocusableInTouchMode(false);
                break;
            case R.id.radio1:
                editText.setEnabled(false);
                editText.setInputType(InputType.TYPE_CLASS_TEXT);
                editText.setFocusableInTouchMode(false);
                break;
        }
    }
});

Upvotes: 0

A. Badakhshan
A. Badakhshan

Reputation: 1043

You should remove this piece of your code:

if ((radioButton.isChecked())) {
    editText.setEnabled(false);
    editText.setInputType(InputType.TYPE_NULL);
    editText.setFocusableInTouchMode(false);
} else {
    editText.setEnabled(false);
    editText.setFocusableInTouchMode(false);
}

and put replace it by:

radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            editText.setEnabled(false);
            editText.setInputType(InputType.TYPE_NULL);
            editText.setFocusableInTouchMode(false);
        } else {
            editText.setEnabled(false);
            editText.setFocusableInTouchMode(false);
        }  
    }
});

This solution will be better if you use CheckBox instead of RadioButton since your need is more compatible with CheckBox.

Upvotes: 0

Jd Prajapati
Jd Prajapati

Reputation: 1971

Use method of setOnCheckedChangeListener and manage checked and uncheck event.

Upvotes: 0

Sagar Gangawane
Sagar Gangawane

Reputation: 1985

Hey first put the two radio button in a Radio group.

Check this code.

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            switch(checkedId)
            {
            case R.id.radio0:
                editText.setEnabled(false);
                editText.setInputType(InputType.TYPE_NULL);
                editText.setFocusableInTouchMode(false);
                break;
            case R.id.radio1:
                 editText.setEnabled(false);
                 editText.setFocusableInTouchMode(false);
                break;
            }
        }
    });

Hope this help.Happy Coding.

Upvotes: 3

Related Questions