4

I'm facing a weird problem when adding a ListView to my layout.

My layout contains 2 EditText and when I start the activity, the keyboard doesn't pop automatically. But when I add a ListView anywhere in the layout, the keyboard pop up when the activity starts.

I know there are many ways to hide the keyboard like this one: getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) and you may have seen other solutions to the problems like here and here but my question is not how to prevent this but why is this happening ?

I have created a simple example to demonstrate this behavior

<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"
    android:background="@android:color/black"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="chadi.projects.code.TestKeyboardActivity" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white" />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@android:color/white" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

</LinearLayout>

If this ListView doesn't exist, the keyboard don't show up. If I replace the ListView by other views, it still doesn't show up. Only when I add a simple ListView (even wihtout populating it with anything), is the keboard showing up.

Why is this happening?

Community
  • 1
  • 1
CAS
  • 545
  • 1
  • 8
  • 15
  • Maybe the `EditText` has the focus when you start your new activity – Tofasio May 14 '15 at 07:40
  • I have tried it with `` on one and/or both `EditText`. The keyboard only pops up when a `ListView` is added anywhere in the layout regarding on where i added the `` – CAS May 14 '15 at 07:51

2 Answers2

1

Try this on your manifest file

<activity
    android:name=".MyActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:windowSoftInputMode="stateHidden" /> 
Kishan Vaghela
  • 7,008
  • 3
  • 36
  • 63
  • You're right, it works! It's the same if we write it in Java like I stated above, but my question focus on ***why*** do we need to explicitly write this when specifically adding a `ListView` to our layout? – CAS May 14 '15 at 08:01
0

for that you have to add request focus to your edit text like this

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:background="@android:color/white">
<requestFocus />
</EditText>
Akash
  • 913
  • 6
  • 14
  • No ! Your solution doesn't work. I don't want to let the keyboard pops up when starting the activity. In addition, even when I add the `requestFocus` , only when I add the `ListView` is the keyboard poping up. – CAS May 14 '15 at 07:43