0

So I have an editText , checkbox and spinner in activity.

When checkbox is checked, editText will shown and spinner will be hidden.

 public void addListenerOnTo() // for checkbox
    {

        checkBoxTo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    travelTo.setVisibility(View.INVISIBLE); // spinner
                    addTo.setVisibility(View.VISIBLE); // editText

                } else {
                    travelTo.setVisibility(View.VISIBLE);
                    addTo.setVisibility(View.INVISIBLE);

                }
            }
        });
    }

How can I type the input straight away after the checkbox is checked without pressing the editText to show the keyboard? Thanks.

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • Possible duplicate of [Android: How to open keyboard for editing EditText when click button?](http://stackoverflow.com/questions/14815696/android-how-to-open-keyboard-for-editing-edittext-when-click-button) – Shamas S Feb 19 '16 at 04:02

2 Answers2

1

This can be simply achieved by setting the focus on the edittext and then making the soft keyboard visible forcefully !

  public void addListenerOnTo() // for checkbox
        {

            checkBoxTo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        travelTo.setVisibility(View.INVISIBLE); // spinner
                        addTo.setVisibility(View.VISIBLE); // editText

    // add this line to make the keyboard visible 

addTo.requestFocus();

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

                    } else {
                        travelTo.setVisibility(View.VISIBLE);
                        addTo.setVisibility(View.INVISIBLE);

                    }
                }
            });
        }

Let me know if this works! :)

Vivek Bhardwaj
  • 530
  • 5
  • 16
0

Add this code after addTo.setVisibility(View.VISIBLE);

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);
Jas
  • 3,207
  • 2
  • 15
  • 45