1

This might look like a redundant and already answered query but I am stuck. I have browsed through previously shared plethora of responses but none of them turned out to be a holistic solution.

Here's how the main activity xml looks like:

<RelativeLayout >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <ScrollView >

        <LinearLayout >

            <android.support.v7.widget.CardView >

                <LinearLayout >

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView >

            </android.support.v7.widget.CardView>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

<android.support.design.widget.NavigationView />

The Problem

There is a edit text view in child linear layout of first card view. As of now, if I click on it, the keyboard pops up but doesn't hide when I click elsewhere or scroll the page or click any other view (there's a drop down).

What I plan to do is to hide the keyboard when, 1. I click anywhere outside the edit text view. 2. Scroll the page. 3. Interact with other views (the drop down).

Possible Solutions Tried

how to dismiss keyboard from editText when placed in scroll view - This one isn't working at all for me.

How to hide soft keyboard on android after clicking outside EditText? - There was one solution from @vida which worked partially, as in when clicked outside, the keyboard did dismiss. But then again, that's only a partial solution to what I am trying to achieve.

I would appreciate if someone could share a solution (or procedure) to sort this one out. Thanks!

Community
  • 1
  • 1
Paritosh
  • 367
  • 5
  • 20

2 Answers2

0

You could try to override the Scroll Views onClickListener to call this method every time the user clicks anywhere on the app. If that doesnt work just set the outer relative layout to be clickable and everytime the layout is clicked you can call this hideKeyboard method!

public void hideKeyboard(View view) {
   InputMethodManager im =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
   im.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Shrey
  • 671
  • 7
  • 14
  • Here's what I did for main xml and tried to implement on click and on focus change listener for both of them but neither worked. ` ` This might be shooting in the dark but I can't seem to figure out a way to handle this. Just trying whatever I come across! – Paritosh Nov 02 '15 at 23:07
0

First, extend EditText and add a snippet of code that helps dismiss the keyboard every time the EditText instances lose their focus

public class MyEditText extends AppCompatEditText {

public MyEditText(Context context) {
    super(context);
    setupEditText();
}

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    setupEditText();
}

public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setupEditText();
}

public void setupEditText() {
    // Any time edit text instances lose their focus, dismiss the keyboard!
    setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus && !(findFocus() instanceof MyEditText)) {
                hideKeyboard(v);
            } else {
                showKeyboard(v);
            }
        }
    });
}

public void hideKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

public void showKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(view, 0);
}
}

Then, set android:clickable="true" and android:focusableInTouchMode="true" in the child layout of your ScrollView!

Please note that, it should be the child layout of ScrollView, not ScrollView itself.

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <MyEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </MyEditText>

</LinearLayout>

</ScrollView>

That should work!

Khang Vu
  • 329
  • 4
  • 13