Why does the soft keyboard show or not when the action begins?

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

When an action begins, in some cases a keyboard appears, but sometimes not.

In fact, without a ScrollView , the soft keyboard does not display on EditText 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" > <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 ScrollView , a soft keyboard appears 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 availability of ScrollView . We can fix this with a special ad in AndroidManifest , but this is the default behavior.

My development colleague and I are wondering why this is happening.

+7
android user-interface android-softkeyboard android-edittext scrollview
source share
3 answers

Here is what I understand about this problem after digging into Android code and creating some test layouts using EditText .

How ScrollView is defined as

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

I tried using FrameLayout as a container for the EditText element. As a result, the soft keyboard does not start.

 <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 ScrollView launches a soft 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> 

Thus, the element that allows you to launch the soft keyboard is located in the ScrollView source file.

Edit: by creating my own MyFrameLayout class that extends FrameLayout and playing with the code, I found that this is something in the default scrollview style ( R.attr.scrollViewStyle ), which is responsible for displaying the keyboard or not ..

Edit2: finally, the android:scrollbars allows android:scrollbars to automatically launch the keyboard at startup, if present ...

+3
source share

In my case android: scrollbar fixed until i added:

 android:windowSoftInputMode="adjustResize"> 

The ability to scroll when the keyboard is displayed.

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

 android:focusableInTouchMode="true" 

In a Scrollview child

I found the focusableInTouchMode answer here: Stop EditText from getting focus when starting Activity

0
source share

This is due to the fact that when the application starts, the android focuses on the first available view. In the first case, this is EditText, so the keyboard pops up. In the second case, the first view is a ScrollView, this is the first view that does not require a keyboard, therefore it is not displayed. In addition, in the first case, you can delete <requestFocus /> , and on some devices the keyboard will not appear. Hope this helps.

-one
source share

All Articles