Maintain keyboard open/closed state for EditText when app comes to foreground
You should use a flag (boolean kbShowing)
to keep the current keyboard status, such as set kbShowing = true
when keyboard show, otherwise set kbShowing = false
.
onCreate
showKB(); // if keyboard is not showed automatically.
onRestart
if(kbShowing)
showKb(); // if keyboard is not showed automatically.
else
hideKb(); // if keyboard is showed automatically.
If you don't know how to detect when keyboard show or hide, chck Stefan's answer on this topic How to capture the "virtual keyboard show/hide" event in Android?
Have you tried adding the keyboard state in the Manifest file in your activity:
android:windowSoftInputMode="stateAlwaysVisible|adjustPan">
This will take care of the rotation part of your issue and should work on onResume as well. The stateAlwaysVisible will start the keyboard on the onCrate and the adjustPan will handle the rotation.
Here is a sample from one of my Activities from my Manifest file:
<activity
android:name=".GMax3Main"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible|adjustPan">
<intent-filter>
<action android:name="com.medeasy.GMax3.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Hope this helps.
In my Activity Class I open my softkeyboard on the onCreate method of my class like so:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new UserSync().execute();
setContentView(R.layout.main);
InputMethodManager imm = (InputMethodManager)
GMax3Main.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.RESULT_SHOWN, 0);
}
Then I call my android:windowSoftInputMode="stateAlwaysVisible|adjustPan">
like shown above in my Manifest file.
Hello first of all thanks for an interesting question. It made me experiment with code. Here I am describing my solution.
To find the solution I had to know two things
1. how to detect whether a softkeyboard is visible or not
2. How to set softkeyboard visible or hidden.
I got the solution in the following steps
after searching a bit I realized that the best solution of detecting a softkeyboardstate
(visible/hidden) is to use ViewTreeObserver. I am directly pointing to a so answer to know about it if you don't know. Here is the link.
and to set the softkeyboardstate
I just used Window.setSoftInputMode
method.
and to know a user interaction I override onUserInteraction
method
Kept two flag. one flag is to preserve keyboardstate
another is to know whether the application went to background or not
CODE:
1. variable declared
int lastDiff = 0;
volatile boolean flag = false;
volatile int flag2 = 0;
2. ViewTreeObserver
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView()
.getHeight() - (r.bottom - r.top);
if (lastDiff == heightDiff)
return;
lastDiff = heightDiff;
Log.i("aerfin","arefin "+lastDiff);
if (heightDiff > 100) { // if more than 100 pixels, its
// probably a keyboard...
flag2 = 0;
} else {
if (flag == false)
flag2 = 1;
}
}
});
3. Handling user interaction
@Override
public void onUserInteraction() {
super.onUserInteraction();
flag = true;
}
4. Finally onPause
and onResume
@Override
protected void onPause() {
super.onPause();
flag = true;
}
@Override
protected void onResume() {
flag = false;
switch (flag2) {
case 0:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
break;
case 1:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
break;
default:
break;
}
super.onResume();
}
Explanation:
Here I used two flags (flag2
and flag
). flag2
preserves the keyboardstate
and flag
preserves whether the application goes to background or is there any user interaction. flag
is used because when application goes to background then at first it hides the keyboard. Other things can be easily understood from the code above.
Test:
tested in s2(ics), desire s (ics), galaxy y (2.3.6)
Final comment:
I wrote the code quickly so might be missed some other optimization. Also there might be chance of exceptional cases. If the screen changes for some reasons other than keyboard then it might not be able to detect keyboard state.
Declare your EditText at Class level ...
EditText editText;
Now override onResume() and onPause() method of activity ...
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
editText.requestFocus();
editText.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}, 100);
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
editText.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}, 200);
}
This code work for me perfectly.
Enjoy - :D