0

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.

2 Answers 2

1

Put this code in your manifest file

<activity
        android:name=".YourActivity"
        android:label="@string/title_activity"
        android:windowSoftInputMode="stateHidden" />
2
  • This seems the most clean solution of all the answers, gonna try it tomorrow. Aug 11, 2016 at 18:47
  • On a second thought, the keyboard does not occur on startup anyway. But the EditText is highlighted (i. e. focused). Aug 11, 2016 at 18:48
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.