31

The SearchView is focused by default, but when I try to show software keyboard - it doesn't happen:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

But when I click on the SearchView - it does. Why?

artem
  • 16,382
  • 34
  • 113
  • 189

11 Answers11

50

Fixed!

mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View view, boolean hasFocus) {
                            if (hasFocus) {
                                showInputMethod(view.findFocus());
                            }
                        }
                    });

And

private void showInputMethod(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
          imm.showSoftInput(view, 0);
        }
    }
artem
  • 16,382
  • 34
  • 113
  • 189
16

I encountered this problem with a SearchView that still would not open the keyboard, and was able to do so by adding a 200ms delay:

   mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(final View view, boolean hasFocus) {
            if (hasFocus) {
                view.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(view.findFocus(), 0);
                    }
                }, 200);
            }
        }
    });
  • 2
    Works and upvote! But in my case the problem was the 'SearchView' location in the actionbar, so if it helps someone...locate your searchView always visible, using 'showAsAction= "always"' in your xml, the keyboard appears immediately. – noe Aug 10 '16 at 19:40
12

If you are using SearchView:

<android.support.v7.widget.SearchView
      android:id="@+id/searchView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:focusableInTouchMode="true"
      android:iconifiedByDefault="true"
      app:showAsAction="always"
      app:iconifiedByDefault="true"/>

Then just add it to your onCreate method :

final SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setFocusable(true);
        searchView.setIconified(false);
        searchView.requestFocusFromTouch();

It works for me.

rzaaeeff
  • 850
  • 1
  • 10
  • 18
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
6

What about using setOnFocusChangeListener?

searchview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        }
    }
});
Fifer Sheep
  • 2,900
  • 2
  • 30
  • 47
  • 1
    Hmmm... what about this? http://stackoverflow.com/questions/10089993/android-how-to-focus-actionbar-searchview – Fifer Sheep Aug 19 '12 at 00:15
5

Best and simplest way to do it :

        android:focusable="true"
        app:iconifiedByDefault="false"
        android:focusableInTouchMode="true"

Add above three lines into your xml under SearchView and see the magic....!!

Prateek Gupta
  • 835
  • 10
  • 18
2

Try:

void focusSearchViewAndOpenOrCloseKeyboard(boolean focus) {
    searchView.setIconified(focus);
}

The source shows us that SearchView handles its own closing and opening of the soft keyboard.

EricRobertBrewer
  • 1,750
  • 1
  • 22
  • 25
0
 <EditText
        android:id="@+id/edt_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:background="@null"
        android:ellipsize="end"
        android:hint="@string/SERACH"
        android:imeOptions="actionSearch"
        android:inputType="textVisiblePassword"
        android:singleLine="true"
        android:textAlignment="viewStart" />
Peter
  • 587
  • 6
  • 16
0

add <requestFocus /> in your search view like this

<EditText
            android:id="@+id/et_search"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@android:color/transparent"
            android:imeOptions="actionSearch"
            android:maxLines="1"
            android:padding="8dp"
            android:singleLine="true"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textColor="@android:color/white"
            android:textColorHint="#e6e6e6">

            <requestFocus />

        </EditText>
Basheer Adel
  • 41
  • 1
  • 5
0

I tried all the solutions, but finally the only one that worked for me is the following:

kotlin version:

mSearchView.viewTreeObserver.addOnGlobalLayoutListener(object: OnGlobalLayoutListener {
    override fun onGlobalLayout() {
        mSearchView.requestFocus()
        val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(mSearchView, 0)
        mSearchView.viewTreeObserver.removeOnGlobalLayoutListener(this)
    }
})

java version:

mSearchView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mSearchView.requestFocus();
        InputMethodManager imm = (InputMethodManager) 
        getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mSearchView, 0);
        mSearchView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
0

I needed the keyboard to automatically appear and have the searchView ready for typing with the cursor blinking.

This is what worked for me. In onViewCreated, I added these 2 specific lines:

binding.searchView.isIconified = false
binding.searchView.requestFocus()

Code:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    binding.searchView.isIconified = false
    binding.searchView.requestFocus()
    // ... 
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
-1

I solved this issue in next simple way.

SearchView menu item:

<item
    android:id="@+id/menu_item_search"
    android:icon="@drawable/ic_search"
    android:title="@android:string/search_go"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView" />

To expand SearchView and show keyboard programmatically call:

mMenuItemSearch.expandActionView();
mSearchView.setQuery("your default query test or null", true);

Tested with different showAsAction flags. As result keyboard does not shown when flag: "always". However everything works with flag: "always|collapseActionView"

expandActionView() + setQuery("null or restored text", true); can be called when view created (or restored) to show keyboard and focus on search. P.S: call mSearchView.clearFocus(); after this two method to clear the focus.

Komal12
  • 3,340
  • 4
  • 16
  • 25
Oleksandr B
  • 3,400
  • 1
  • 25
  • 28