in Technology by

I want to automatically show the soft-keyboard when an EditText is focused (if the device does not have a physical keyboard) and I have two problems:

  1. When my Activity is displayed, my EditText is focused but the keyboard is not displayed, I need to click again on it to show the keyboard (it should be displayed when my Activity is displayed).

  2. And when I click done on the keyboard, the keyboard is dismissed but the EditText stays focused and y don't want (because my edit is done).

To resume, my problem is to have something more like on the iPhone: which keep the keyboard sync with my EditText state (focused / not focused) and of course does not present a soft-keyboard if there is a physical one.

1 Answer

0 votes
by

To force the soft keyboard to appear, you can use

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

And for removing the focus on EditText, sadly you need to have a dummy View to grab focus.

I hope this helps


To close it you can use

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

This works for using it in a dialog

public void showKeyboard(){
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

public void closeKeyboard(){
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

Related questions

0 votes
    I am using Android Data Binding framework I have suppose an EditText for login form with username as below ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I have a screen that has textviews and edittexts. I am trying to edittexts equal width. But they look ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    How can we make a UITextField move up when the keyboard is present - on starting to edit?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    I've created a searchable activity as described here: http://developer.android.com/guide/topics/search/ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    I have a view with a text field in it, but the text field is at the bottom. When I click the keyboard, it pops up ... to push the rest of the view up when a keyboard comes up?...
asked Mar 9, 2021 by JackTerrance
0 votes
    When are the keyboard events fired? (a) When the user manually calls the button (b) When the user ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Which among the following is more focused on securing networks? Network Security Anti-virus Endpoint Security Firewall...
asked Oct 25, 2020 in Technology by JackTerrance
0 votes
    Find out various Supreme Court cases that have focused on the rights and liberty of Indian citizens. Please answer the above question....
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    Name two Indian reformers who focused on gender inequality. Please answer the above question....
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    Name 4 areas that five-year plans focused on. Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    I have a problem that I suspected would be a lot simpler than it was. I'm trying to draw a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    When is an application said to show a web performance? (a) Time to respond (b) Time to load (c) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    When is an application said to show a runtime performance? (a) Speed of response to user (b) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 21, 2021 in Education by JackTerrance
...