Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the soft keyboard shows or not when an activity starts?

When comparing our design between developers, we found a strange behavior. After some analysis we went to this observation.

When the activity starts, on some cases the keyboard appears but sometimes not.

In fact, without a ScrollView, the soft keyboard does not appear by default on an EditText.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />
    </EditText>
</LinearLayout>

But when we add a ScrollView, the soft keyboard shows up by default.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="text" >
            <requestFocus />
        </EditText>
    </ScrollView>
</LinearLayout>

It only depends on the presence of the ScrollView. We can fix that with a specific declaration in the AndroidManifest, but this is the default behavior.

I and my fellow developer wonder why is this occurring ?

like image 233
Solostaran14 Avatar asked Sep 10 '13 14:09

Solostaran14


People also ask

How can I tell if my keyboard is open or soft?

Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.

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.


1 Answers

Here is what I understand of this problem after digging in the code of Android and building some test layouts with an EditText.

As ScrollView is defined as

 public class More ...ScrollView extends FrameLayout { ... }

I tried using a FrameLayout as a container for an EditText item. As a result the software keyboard is not triggered.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text" >
        <requestFocus />
    </EditText>
</FrameLayout>

But as written in the question, using a ScrollView triggers the software keyboard (I simplified the xml source).

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text" >
        <requestFocus />
    </EditText>
</ScrollView>

So the element that allows the software keyboard to be triggered is in the ScrollView source file.

Edit: after having created my own class MyFrameLayout extending FrameLayout and playing with the code, I found that it is something in default scrollview style (R.attr.scrollViewStyle) that is responsible for the keyboard to be shown or not...

Edit2: finally the attribute android:scrollbars allows the keyboard to be automatically triggered at startup if present...

like image 78
Tok' Avatar answered Nov 15 '22 01:11

Tok'