1

I have a layout of the following structure (some items and attributes omitted for brevity):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical">

    <ScrollView android:orientation="vertical">

        <TableLayout android:layout_width="match_parent"
            android:layout_height="wrap_content">

            ...

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Label" android:textAppearance="?android:attr/textAppearanceMedium"/>

                <RelativeLayout
                    android:descendantFocusability="beforeDescendants"
                    android:focusableInTouchMode="true"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/btnSaveStep"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:text="Save"/>

                    <EditText
                        android:id="@+id/etStep"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toLeftOf="@id/btnSaveStep"
                        android:ems="10"
                        android:textAlignment="viewEnd"
                        android:inputType="numberDecimal"
                        android:selectAllOnFocus="true"
                        android:text="10"/>

                </RelativeLayout>

            </TableRow>

            ...

        </TableLayout>
    </ScrollView>

    <Button
        android:id="@+id/btnProceed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Proceed"/>
</LinearLayout>

Note the android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true" attributes of the RelativeLayout - they had no effect. I also tried this solution, and I placed that dummy layout in numerous places of the layout - all to no avail.

Community
  • 1
  • 1
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

2 Answers2

1

I had the same issue and literally tried everything. This is what worked for me in the end.

 private class EditTextFocusListener implements View.OnFocusChangeListener {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (v.equals(inputEmail)) {
            if (!hasFocus) hideKeyboard(inputEmail);
        } else if (v.equals(inputPassword)) {
            if (!hasFocus) hideKeyboard(inputPassword);
        }
    }

    private void hideKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}

  EditTextFocusListener focusListener = new EditTextFocusListener();
  inputEmail.setOnFocusChangeListener(focusListener);
  inputPassword.setOnFocusChangeListener(focusListener);
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
1

Put this code in your manifest file

<activity
        android:name=".YourActivity"
        android:label="@string/title_activity"
        android:windowSoftInputMode="stateHidden" />