I've noticed in the Android Market Application
, when you click over the search button, it shows the keyboard, but when you click the back
button, the search EditText
becomes invisible and the keyboard
is hidden. The problem is that I can't hide the EditText
after the keyboard is hidden after pressing the back key because I can't find a listener for hiding the keyboard event.
I found this sample How to capture the "virtual keyboard show/hide" event in Android?
but it doesn't work on the soft keyboard.

- 2,242
- 4
- 15
- 33

- 12,304
- 8
- 54
- 77
-
I might get it wrong, but how about hiding the keyboard first and then making the EditText invisible? – banzai86 Oct 07 '11 at 07:21
-
@banzai86 yes thats what am trying to do. – Mohammad Ersan Oct 07 '11 at 22:53
5 Answers
You need to implement this to capture the BACK button before it is dispatched to the IME:

- 90,665
- 16
- 140
- 154
-
I second the awesome. After several hours of trying various focus and editor actions, this did the trick perfectly. – Geobits Sep 21 '12 at 01:15
-
This will work on Edittext? That means I can use on Edittext without create a subclass of it? – Mahendran Sep 24 '12 at 13:19
I think you should handle this using focus:
final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
edttext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!(hasFocus))
{
mgr.hideSoftInputFromWindow(edttext.getWindowToken(), 0);
}
}
});

- 5,481
- 2
- 40
- 53
Hey i think the market app is using the googleSearch dialog (check out Searcheable activity ).
You can implement the editText in a popupWindow, and set the poupwindow as focusable. Show the keyboard when your popup is shown. in onDismiss hide the keyboard.
popupWindow.setFocusable(true);
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
inputMethodManager.hideSoftInputFromWindow(
edttxtSearchBar.getWindowToken(), 0); }
This will ensure, you click anywhere outside popup or press back the popup disappears as well(along with keyboard).

- 4,875
- 3
- 25
- 30
-
the problem is, when the keyboard is shown, and you click on `Back` key, the keyboard will handle the back event, and be hidden, then you must click `Back` again to dismiss the `PopupWindow` or `Dialog` – Mohammad Ersan Oct 07 '11 at 22:56
-
i guess you might also want to override back button and dismiss the popup manually. – rDroid Oct 11 '11 at 04:21
The google market application is using Fragments via the API Support Package. When you click back it is actually going back in the fragment stack. It's like going back an activity without the screen swipe. The fragment that they go back to does not contain the search box which is why it disappears.

- 4,751
- 1
- 25
- 26
**perfect answer** REFER THIS **SIMPLE EXAMPLE**...ITS TOOOO GOOOODDDD
KTBEditTextWithListener.java // Custom edittext
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
public class KTBEditTextWithListener extends android.widget.EditText {
public KTBEditTextWithListener(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public KTBEditTextWithListener(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// createFont(context);
}
public KTBEditTextWithListener(Context context, AttributeSet attrs) {
super(context, attrs);
// createFont(context);
}
private BackPressedListener mOnImeBack;
/* constructors */
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if (mOnImeBack != null) mOnImeBack.onImeBack(this);
}
return super.dispatchKeyEvent(event);
}
public void setBackPressedListener(BackPressedListener listener) {
mOnImeBack = listener;
}
public interface BackPressedListener {
void onImeBack(KTBEditTextWithListener editText);
}
}
//my_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.ktb.gopharma.views.KTBEditTextWithListener
android:id="@+id/edit_text"
style="@style/match_width">
</com.ktb.gopharma.views.KTBEditTextWithListener>
</LinearLayout>
//MyActivity.java
package com.ktb.gopharma;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import com.ktb.gopharma.views.KTBEditTextWithListener;
import com.ktb.gopharma.views.KTBEditTextWithListener.BackPressedListener;
import com.ktechbeans.gopharma.R;
public class MyActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
KTBEditTextWithListener editText = (KTBEditTextWithListener) findViewById(R.id.edit_text);
editText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
showToast("keypad opn");
}
});
editText.setBackPressedListener(new BackPressedListener() {
@Override
public void onImeBack(KTBEditTextWithListener editText) {
showToast("keypad close");
}
});
}
}

- 121
- 12
-
replacing the link with actual code is better .. but still not perfect: Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, _tailor your answers to the question_ – kleopatra Jul 24 '15 at 07:46