Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a few EditText fields in a ListView. When I tap on one of the EditText fields, the keyboard slides into view (as it should), but the EditText field I tapped loses focus. I've tried using various InputMethodManager methods to make the keyboard start out in view (in order to get around the problem rather than truly solve it), but that didn't work - the keyboard was not in view when the Activity appeared.

The EditText's type is number, and when the keyboard is sliding in, it is a number keyboard, but when it finishes sliding and the EditText loses focus, it changes to the alphabetical keyboard (which reinforces the idea that the EditText no longer has focus).

My questions are these:

1) How can I make the selection of my EditText field and the subsequent sliding in of the soft keyboard not make my EditText lose focus?

... failing that...

2) How can I make the keyboard start out in view so it never has to slide in (thus avoiding the behavior I find so objectionable)?

My manifest does include android:windowSoftInputMode="stateAlwaysVisible", but the keyboard does not appear until I tap on an EditText. This ignoring of the 'stateAlwaysVisible' attribute seems to only occur in the emulator - on my provisioned device, it is honored so question number 2 above does work on the device... but not in the emulator.

Thanks for any help you can provide!

like image 870
Kyle Humfeld Avatar asked Apr 10 '11 23:04

Kyle Humfeld


People also ask

How do I show soft keyboard when EditText is focused?

android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File. edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears.

How do I hide the soft keyboard on Android after clicking outside EditText?

and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);


2 Answers

You need to change in your AndroidManifest.xml

Add android:windowSoftInputMode="adjustPan" in the activity holding the listview. This will solve your problem.

    <activity android:name=".MyEditTextInListView"               android:label="@string/app_name"               android:windowSoftInputMode="adjustPan"> 

Regards

like image 111
Frank Avatar answered Oct 04 '22 21:10

Frank


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.

like image 44
SoloPilot Avatar answered Oct 04 '22 22:10

SoloPilot