5

i have custom listview with editText and edit the edittext data on tapping edittext with help of showing keypad it is working fine .

my problem is when i click outside of edittext the keypad must hide.

thanks...

bharath
  • 14,283
  • 16
  • 57
  • 95
Archana
  • 509
  • 2
  • 6
  • 20

4 Answers4

5

For this you have to take the onTouchListener on the parent layout of the Layout File. on the TouchListener you have to code to hide the Keyboard when click outside the EditText. Please follow XML Layout and Java class to resolve this issue please follow following url.

http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html

Amit Thaper
  • 2,117
  • 4
  • 26
  • 49
2

One way is that you can set a focus change listener to the EditText. When the widget loses focus you can hide the Keyborad by:-

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
amiekuser
  • 1,630
  • 1
  • 19
  • 31
  • et.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Log.d("onChange", "focus==" + hasFocus); }}); – Archana Oct 12 '11 at 07:09
  • when i click outside edittext like listview then it doesn't response – Archana Oct 12 '11 at 07:10
  • 2
    edittext will not change focus when clicking outside of it with setOnFocusChangeListener. It will only work when changing focus to something else that's focusable, like another edittext. – Karl Jul 25 '12 at 13:28
0

You can achieve this by doing the following steps:

  1. Make the parent view(content view of your activity) clickable and focusable by adding the following attributes

        android:clickable="true" 
        android:focusableInTouchMode="true" 
    
  2. Implement a hideKeyboard() method

        public void hideKeyboard(View view) {
            InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    
  3. Lastly, set the onFocusChangeListener of your edittext.

        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    hideKeyboard(v);
                }
            }
        });
    

From This source

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
0

Another way similar to the accepted answer is to subclass the root view of the layout and override OnInterceptTouchEvent, hide the keyboard and return false to allow the touch to propagate as usual.

The example below is for Xamarin but it is easy to port to Java:

public class KeyboardHidingScrollView : ScrollView
{
    public KeyboardHidingScrollView (Context context) : base (context)
    {
    }

    public KeyboardHidingScrollView (Context context, IAttributeSet attrs) : base (context, attrs)
    {
    }

    public override bool OnInterceptTouchEvent (Android.Views.MotionEvent ev)
    {
        var methodManager = (InputMethodManager)Context.GetSystemService (Context.InputMethodService);
        methodManager.HideSoftInputFromWindow (WindowToken, HideSoftInputFlags.None);
        return false;
    } 
}
Ryan
  • 2,109
  • 1
  • 15
  • 19