Home » Android » android – Keyboard hide event with BACK key

android – Keyboard hide event with BACK key

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:

You need to implement this to capture the BACK button before it is dispatched to the IME:

http://developer.android.com/reference/android/view/View.html#onKeyPreIme(int, android.view.KeyEvent)

Answer:

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

        }
    });

Answer:

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).

Answer:

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.

Answer:

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

            }