Skip to content Skip to sidebar Skip to footer

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 app

Solution 1:

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

publicclassMore ...ScrollView extendsFrameLayout { ... }

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

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid: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).

<ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid: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...

Solution 2:

In my case android:scrollbars fixed this until I had to add:

android:windowSoftInputMode="adjustResize"> 

To be able to scroll when keyboard shows.

To be able to use both properties I had to add:

android:focusableInTouchMode="true"

In the child of the Scrollview

I found focusableInTouchMode answer here: Stop EditText from gaining focus at Activity startup

Solution 3:

This is because when and app is launched, android focuses on the first available view. In the first case it is the EditText, thats why the keyboard pops up. In the second case, the first view is the ScrollView is the first view, which doesn't require keyboard, so it is not shown. Also, in the first case, you can remove <requestFocus />, and on some devices, the keyboard, will not pop up. Hope this helps.

Post a Comment for "Why The Soft Keyboard Shows Or Not When An Activity Starts?"