Soft Keyboard Shows Up on Edittext Focus Only Once

Soft Keyboard shows up on EditText focus ONLY once

Try to open and hide inside a Runnable as,

TO OPEN

                 ettext.requestFocus();
ettext.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);

TO CLOSE

                    ettext.requestFocus();
ettext.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(ettext.
getWindowToken(), 0);
}
},200);

How to show soft-keyboard when edittext is focused

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.


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

Android: show soft keyboard automatically when focus is on an EditText

You can create a focus listener on the EditText on the AlertDialog, then get the AlertDialog's Window. From there you can make the soft keyboard show by calling setSoftInputMode.

final AlertDialog dialog = ...;

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});

EditText loses focus when keyboard appears; requires touching twice to edit

A solution is to post a delayed runnable that checks to see if the EditText view is still focused, and if it is not, focus it.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(final View v, boolean hasFocus) {
if (hasFocus) {
// Request focus in a short time because the
// keyboard may steal it away.
v.postDelayed(new Runnable() {
@Override
public void run() {
if (!v.hasFocus()) {
v.requestFocus();
}
}
}, 200);
}
}
});

How can I set the focus (and display the keyboard) on my EditText programmatically

Try this:

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

http://developer.android.com/reference/android/view/View.html#requestFocus()

Stop EditText from bringing up the keyboard on Activity start?

Alternatively you can set the focus to the root layout element:

android:focusable="true"
android:focusableInTouchMode="true"

Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">

<EditText
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

When the soft keyboard appears, it makes my EditText field lose focus

Here is how I did it. The onFocusChangeListener() is called several times when you touch a EditText to type text into it. The sequence is:

  1. If focus was on a different view, then that view loses focus
  2. The target gains focus
  3. Soft keyboard pops up.
  4. This causes the target to lose focus
  5. The code detects this situation and calls target.requestFocus()
  6. The leftmost, topmost view gains focus, due to Android nonsense
  7. The leftmost view loses focus, due to requestFocus being called
  8. Target finally gains focus

    //////////////////////////////////////////////////////////////////
    private final int minDelta = 300; // threshold in ms
    private long focusTime = 0; // time of last touch
    private View focusTarget = null;

    View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
    long t = System.currentTimeMillis();
    long delta = t - focusTime;
    if (hasFocus) { // gained focus
    if (delta > minDelta) {
    focusTime = t;
    focusTarget = view;
    }
    }
    else { // lost focus
    if (delta <= minDelta && view == focusTarget) {
    focusTarget.post(new Runnable() { // reset focus to target
    public void run() {
    focusTarget.requestFocus();
    }
    });
    }
    }
    }
    };

The code above works well for the keyboard pop-ups. However, it does not detect the speech-to-text pop-up.

Why my EditText view cannot bring up soft keyboard in Android even I force it to show?

I think it's not enough to just request focus. Please try adding following code to your onCreate method:

if(editText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

I hope this helps you!

Android: force keyboard to appear and focus on EditText

Thanks to @Shubham's link I was able to figure it out. The solution was not the answer given in the link, however. It was the second answer.

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

Edit:

once using the above solution the keyboard will remain on the screen until a user presses either the back button or the home button (sometimes it takes a few times). To remove the keyboard use this

imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0);

in my case rootView is the rootView of the current activity. I have not tested this to see if this will work on child views.



Related Topics



Leave a reply



Submit