0

I have a searchview widget in my activity that is not part of a toolbar. In my onCreate method I use searchView.setIconified(false); and that does bring the searchView into focus, but it does not bring up the keyboard unless I click it again. How do I have they keyboard also pop up?

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:background="@color/lighterGrey">
    <android.support.v7.widget.SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:transitionName="@string/search_transition"
        android:background="@drawable/search_shape"/>
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/acronym_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

java:

searchView = (SearchView) findViewById(R.id.search_view);
searchView.setIconified(false);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
View view = this.getCurrentFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
    imm.showSoftInput(view, 0);
}
DessertsAndStuff
  • 185
  • 2
  • 15

2 Answers2

0

You need to update your AndroidManifest.xml. Add android:windowSoftInputMode="stateVisible|adjustPan" to your Activity declaration. For example:

    <activity
        android:name=".MyActivity"
        android:windowSoftInputMode="stateVisible|adjustPan">
    </activity>

stateVisible will show the software keyboard as soon as the Activity starts. adjustPan will adjust the view to compensate for the keyboard being shown (so that the content you're trying to show isn't covered by the keyboard).

Eric Bachhuber
  • 3,633
  • 2
  • 16
  • 24
  • [Have you tried showing the keyboard programmatically?](https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1) If that doesn't work, post code & XML so we can help you with your issue – Eric Bachhuber Jul 26 '17 at 21:36
  • Did you try setting focusableInTouchMode = true in the layout? – Eric Bachhuber Jul 27 '17 at 15:31
0

Try to add the following

searchView.setFocusable(true); searchView.setIconified(false); searchView.requestFocusFromTouch();

It should focus and open keyboard. In some devices, you'll have to add also

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

if (imm != null) {
imm.showSoftInput(view, 0);
}

BNAndroid
  • 150
  • 4