Moin Khan Moin Khan - 4 months ago 46
Android Question

How to get Focus on EditText after checked CheckBox?

When user launch app then I don't want to allow user to edit in EditText. Now what I want that when user click on checkBox then user able to edit Edittext but below is my code and when I checked the checkbox it is not giving focus. how can I achieve this ?

cbIsGap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {

editText1.setFocusable(true);
editText1.setClickable(true);
editText2.setClickable(true);
editText2.setFocusable(true);


}else {


editText1.setFocusable(false);
editText1.setClickable(false);
editText2.setFocusable(false);
editText2.setClickable(false);

}
}
});

Answer
editText.setFocusableInTouchMode(true); 
editText.requestFocus();