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 ?
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With