user5527692
user5527692

Reputation:

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);

                }
            }
        });

Upvotes: 0

Views: 1427

Answers (4)

Moulesh
Moulesh

Reputation: 2810

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

Upvotes: 1

Vivek Mishra
Vivek Mishra

Reputation: 5705

According to my experience with checkboxes, onClickListener provide reliable results as compared to checkedChangeListener. So I would suggest to use clickListener like this

checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(checkbox.isChecked())
    {
    txtConsAcNo.setFocusable(true);
                    txtConsAcNo.setClickable(true);
                    txtMeterSrMo.setClickable(true);
                    txtMobile.setFocusable(true);
    }else{
                    txtConsAcNo.setFocusable(false);
                    txtConsAcNo.setClickable(false);
                    txtMeterSrMo.setFocusable(false);
                    txtMeterSrMo.setClickable(false);
    }
            }
        });

Upvotes: 0

Jake
Jake

Reputation: 26

You should use EditText.requestFocus(); MethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);

Upvotes: 0

Palak
Palak

Reputation: 2215

try this when checked CheckBox

txtMobile.requestFocus();

Upvotes: 0

Related Questions