2

I have a LinearLayout composed by a ListView(which loads from an AsynTask) and an EditText(so I can filter that list). Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Filtrar por nombre..." >

    </EditText>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

The problem is that when the activity starts, the keyboard is shown up and I only want to show it when the EditText is clicked for a search.

I've tried several methods such as writing the following in the manifest

android:windowSoftInputMode="stateHidden"

and including this in the class

InputMethodManager iMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
iMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);

The only thing I've achieved is to not show the keyboard but not showing the loaded stuff of the list(just the EditText and the rest of the screen plain black), and once I click on the EditText, they appear both keyboard and the stuff of the listView.

Any posible solution for this?

EDIT:

I've tried the following solutions(thanks to @Sush, @boztalay and @Agata Sworowska), but none did the trick.

-Placed in my layout:

android:focusable="true"
android:focusableInTouchMode="true"

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

-And placed in my onCreate() method:

ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();

Any other posibility?

NEW EDIT:

I've noticed that when it doesn´t focus on the EditText, the listView doesn´t load until I focus back the EditText, llike if it was waiting for it. I've tried to set up the EditText after the code that loads the ListView but the problem persists.

ikso
  • 61
  • 2
  • 9

4 Answers4

1

This sounds like an issue with focus. What's happening is when the app is started, the EditText gets focus, and the keyboard pops up. You should try to make it so that the ListView gets focus. This way, the keyboard won't pop up until the user gives the EditText focus when they click on it.

In your Java, you should get an instance of the ListView, then call a couple of functions:

ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();

If you put that in your onCreate(), focus will be given to the ListView, and they keyboard won't come up until the user clicks the EditText.

See this SO question for more details.

Community
  • 1
  • 1
boztalay
  • 562
  • 6
  • 17
  • I'm sorry to say this didn´t work either. With this code placed where you said(in my onCreate method), when the layout loads, the keyboard appears. – ikso Oct 08 '12 at 22:41
0

You can just add below two attributes to your LinearLayout.

android:focusable="true"
android:focusableInTouchMode="true"
Sush
  • 6,839
  • 1
  • 18
  • 26
  • This would be a better route to go, but you'll still have to call requestFocus() for the ListView to actually get focus. – boztalay Oct 08 '12 at 18:21
  • @boztalay : Is there a need of focus for the ListView? The linear layout will just keep the focus to avoid the EditText getting focus by default. So I feel these two lines should do the trick. – Sush Oct 08 '12 at 18:24
  • I don't think the ListView needs focus, but the EditText will automatically request it, so that behavior needs to be stopped somehow. Those two lines will only make it so that the list view can have focus. – boztalay Oct 08 '12 at 18:27
  • I've placed your code as you said but I'm afraid that only made nothing to appear. It's true the EditText wasn't focused, but the ListView didn't show. – ikso Oct 08 '12 at 22:30
0

Try adding in Your layout, that contains EditText this:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

I hope it helps.

Agata
  • 399
  • 3
  • 6
  • I've placed your code as you said, inside my LinearLayout, but I'm afraid that only made nothing to appear. It's true the EditText wasn't focused, but the ListView didn't show. – ikso Oct 08 '12 at 22:35
0

http://www.mysamplecode.com/2012/02/android-edittext-hide-soft-keyboard.html

This is a solution I found for the issue here. Do not understand why listivew should do this, but the workaround helps.

  • I'm afraid this hasn´t do the job either. I guess thats because instead of a button(like in the example), I have a listView which needs to load, since it doesn´t appear until I press/focus the editText. Thank you anyway for trying! – ikso Feb 03 '13 at 09:15