Chris
Chris

Reputation: 369

How can I keep the soft keyboard up when switching to the next EditText in a recycler view?

The problem I am facing is when the user clicks the next button on the keyboard, the program should focus onto the next EditText on the screen and keep the keyboard open until there are no more enabled EditTexts on the screen. When there are no more enabled EditTexts, the keyboard should disappear.

Another issue is when there is an EditText that isn't currently visible on screen then it will not gain focus until the user selects it.

I have tried using the Input Method Manager to show the keyboard when the EditText has focus and hide when there is none. If there are no more enabled EditTexts enabled, the keyboard is still present.

final InputMethodManager imm = (InputMethodManager) 
context.getSystemService(Context.INPUT_METHOD_SERVICE);

    if (variable.getType().equals("Value")) {
        if (variable.getFormat().equals("Number") || variable.getFormat().equals("2Number")) {
            viewHolder.inputValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if(hasFocus){
                        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                    } else if(hasFocus && !viewHolder.inputValue.isEnabled()){
                        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }
                }
            });
        } else if (variable.getFormat().equals("Text")) {
            viewHolder.messageText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if(hasFocus){
                        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                    } else if(hasFocus && !viewHolder.inputValue.isEnabled()){
                        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }
                }
            });
        }
    } else if (variable.getType().equals("Message")) {
        viewHolder.messageText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if(hasFocus){
                    imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                } else if(hasFocus && !viewHolder.inputValue.isEnabled()){
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
        });
    } else {
        //imm.hideSoftInputFromWindow(viewHolder.itemView.getWindowToken(), 0);
    }

The EditTexts are stored within cardviews inside a recyclerview. Each card has a variable type. Only "Value" and "Message" variable types should receive focus when clicking the next button on the keyboard.

I expect the user to be able to scroll through enabled EditTexts within the RecyclerView by clicking the next button on the keyboard. If an EditText is not within view on screen the screen should scroll down to it to gain focus. Also, if the EditText is disabled it should never gain focus.

Upvotes: 3

Views: 205

Answers (1)

Sapnesh Naik
Sapnesh Naik

Reputation: 11656

Override the onEditorAction method and return true for each editText you want to keep the keyboard open.

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
          if (actionId == EditorInfo.IME_ACTION_DONE) {
             //optional: you can run some logic here.
          }
          //return true to not hide the keyboard
          return true;
      }
});

Upvotes: 1

Related Questions