Harsha M V
Harsha M V

Reputation: 54949

EditText focus opens Keyboard automatically

When i start Login Activity. it has two fields email and password. Email should get the focus but not open the keyboard automatically when the activity starts.

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="10dip"
        android:layout_marginTop="10dip"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/SplashLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="40dip"
            android:src="@drawable/splash_logo" />

        <EditText
            android:id="@+id/EmailAddress"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:hint="Email Address"
            android:maxLines="1" android:nextFocusRight="@+id/Password">


        </EditText>

        <EditText
            android:id="@+id/Password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="5dip"
            android:hint="Password"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/Login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="10dip"
            android:background="@drawable/login_button_selector"
            android:text="@string/login" />

        <TextView
            android:id="@+id/ForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:layout_marginTop="10dip"
            android:text="@string/forgot_password"
            android:textColor="#000000"
            android:textSize="7pt"/>
    </LinearLayout>

</ScrollView>

Upvotes: 2

Views: 4528

Answers (2)

PeteH
PeteH

Reputation: 1930

This technique suppresses the soft keyboard until the user touches an EditText

InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(YourClass.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {  // protection
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
}

Upvotes: 1

alex.zherdev
alex.zherdev

Reputation: 24164

You should add the attribute

android:windowSoftInputMode="stateAlwaysHidden"

to you activity in AndroidManifest.xml

Upvotes: 7

Related Questions