Search code examples
androidscrollviewandroid-softkeyboard

scrollview within linearlayout causing soft keyboard to appear


I had a linerlayout that contained a number of edittext fields. This all worked as expected in portrait mode with, in particular, the soft keyboard not appearing until one f the edittext fields was tapped.

However, when the device was rotated to landscape mode the bottom few edittexts disappeared off the bottom of the screen and there was no way of accessing them.

The solution seemed simple, to add a scrollview under the topmost linearlayout and to create a linearlayout to contain all of the edittexts.

This fixes the scrolling issue but now the first edittext automatically gets the focus and the soft keyboard pops up.

I've tried setting the focus to a hidden textview and explicitly hiding the keyboard but nothing stops the keyboard from being displayed.

Here is (most of) the layout xmal file in question:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:orientation="vertical"
    android:showDividers="none"
    android:background="#ffffff"
    android:paddingTop="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

...

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Subject"
            android:id="@+id/textView3"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/subject"
            android:editable="false"
            android:ellipsize="end"
            android:singleLine="true"
            android:textStyle="bold"
            android:inputType="textCapWords"
            android:background="@drawable/apptheme_edit_text_holo_light" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Event Text"
            android:id="@+id/textView4"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:id="@+id/text"
            android:linksClickable="false"
            android:enabled="true"
            android:editable="true"
            android:capitalize="sentences"
            android:nestedScrollingEnabled="true"
            android:minLines="3"
            android:gravity="top"
            android:background="@drawable/apptheme_edit_text_holo_light" />

...

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Additional Comments"
            android:id="@+id/textView6"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:id="@+id/comments"
            android:minLines="3"
            android:gravity="top"
            android:editable="true"
            android:nestedScrollingEnabled="true"
            android:textAlignment="gravity"
            android:background="@drawable/apptheme_edit_text_holo_light" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/id"
            android:visibility="gone" />
    </LinearLayout>
</ScrollView>

Can anyone tell me what I'm doing wrong or, alternatively, how to suppress the keyboard until an edittext is tapped.

Many thanks


Solution

  • Add this inside the activity tag (of the Android Manifest) in which you are having the problem:

    android:windowSoftInputMode="stateHidden"
    

    You can do it programmatically like this:

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);