Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent keyboard from opening when activity is opened in android?

In my android app, in my profile edit page, when I start the activity, the first edittext field is focused (blinking cursor), and the keyboard gets displayed.

How can I keep it being focused on startup (blinking cursor) but prevent the keyboard from showing up? If that is not possible, then just not focus the edittext on startup.

Thanks.

        <EditText
            android:id="@+id/textinput_firstname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="test" />
like image 660
omega Avatar asked Jul 18 '13 00:07

omega


People also ask

How do I hide the activity on my keyboard?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.

How do I make the keyboard go away on my Android?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen. To bring the keyboard back into view, tap the typing area.

How do I get rid of soft keyboard on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I prevent the soft keyboard from pushing my view up in fragment?

Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.


2 Answers

Just add this line of code in your onCreate(...) method

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
like image 50
Salman Khakwani Avatar answered Oct 21 '22 07:10

Salman Khakwani


This is probably happening for EditText for most users, all you have to do is, just go to the layout file and set the layout's view groups attributes as follows:

 android:focusable="true"
 android:focusableInTouchMode="true"

For Example.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:focusable="true"
    android:focusableInTouchMode="true"> 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="41dp"
      />
</LinearLayout>

Note: you have to set the attributes for the first view in the layout no matter what it is This will set the default focus to false;

like image 29
Harshit Jain Avatar answered Oct 21 '22 09:10

Harshit Jain