46

When my Activity with a ScrollView layout and EditTexts starts, the EditTexts get focus and the Android OnScreen keyboard opens.

How I can avoid that?

When I was using LinearLayout and RelativeLayout without the ScrollView it doesn't happen.

I've tried it this way, and it works, but it's not a good way to do it:

TextView TextFocus = (TextView) findViewById(R.id.MovileLabel);
TextFocus.setFocusableInTouchMode(true);
TextFocus.requestFocus();

Next you have an example of some of my layouts with this problem, when this Activity starts, focus goes to the first EditText, Description and the Android keyboard opens automatically, this is very annoying.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:padding="10px">

        <RelativeLayout
            android:gravity="center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/UserLabel" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="13px"
                android:text="@string/userlabel"/>
            <TextView
                android:id="@+id/User"
                android:layout_alignBaseline="@id/UserLabel"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test"/>
        </RelativeLayout>

        <View
            android:layout_gravity="center_horizontal"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#808080"
            android:layout_marginTop="5px"
            android:layout_marginBottom="12px"/>

        <RelativeLayout
            android:gravity="center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
            android:id="@+id/DescriptionLabel" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/desclabel"
            android:layout_marginTop="13px"/>
            <EditText 
            android:id="@+id/Description"
            android:layout_alignBaseline="@id/DescriptionLabel"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="180px"/>
        </RelativeLayout>

        <RelativeLayout
            android:gravity="center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/EmailLabel" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/emaillabel"
                android:layout_marginTop="13px"/>
            <EditText 
                android:id="@+id/Email"
                android:layout_alignBaseline="@+id/EmailLabel"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:width="180px"/>
        </RelativeLayout>

        <RelativeLayout
            android:gravity="center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/MovilePhoneLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/movilephonelabel"
                android:layout_marginTop="13px"/>
            <EditText 
                android:id="@+id/MovilePhone"
                android:layout_alignBaseline="@+id/MovilePhoneLabel"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:width="180px"/>
        </RelativeLayout>

        <View
            android:layout_gravity="center_horizontal"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#808080"
            android:layout_marginTop="5px"
            android:layout_marginBottom="10px"/>


        <RelativeLayout
            android:gravity="center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/applybutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/apply"
                android:width="100px"
                android:layout_marginLeft="40dip"/>
            <Button
                android:id="@+id/cancelbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cancel"
                android:width="100px"
                android:layout_alignBaseline="@+id/applybutton"
                android:layout_alignParentRight="true"
                android:layout_marginRight="40dip"/>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • i test it and yes, if i remove the scrollview from my layout, the android keyboard doesn'ts open automatically!!! the problem it's for the scrollview, but i need it :S can someone help? – NullPointerException Nov 10 '10 at 22:16

5 Answers5

124

Android opens the OnScreenKeyboard automatically if you have an EditText focussed when the Activity starts.

You can prevent that by adding following into your Activity's onCreate method.

 getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Juhani
  • 5,076
  • 5
  • 33
  • 35
27

If you want to do it editing the AndroidManifest:

        <activity
        android:name=".Dades"
        android:label="@string/app_name" 
        android:windowSoftInputMode="stateHidden">

the line android:windowSoftInputMode="stateHidden" is the one that prevents the keyboard focus.

Pere BG
  • 618
  • 7
  • 9
19

Another way is by adding on LinearLayout:

android:focusable="true"

android:focusableInTouchMode="true"

Let me indicate that

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

still does the same thing, but the cursor is still there.

King of Masses
  • 18,405
  • 4
  • 60
  • 77
Chris K.
  • 258
  • 3
  • 9
1

This will an inappropriate behaviour

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

but it will do it smoothly(do it in manifest for that activity)

 android:windowSoftInputMode="stateHidden"
ASHu2
  • 2,027
  • 15
  • 29
0

You can use below statement also,

(EditTextName).setInputType(0)

it will permanently hides default keyboard of perticular Edittext, even on EditText Touch or Click.

Addon_Android
  • 271
  • 1
  • 2
  • 9