Search code examples
androidsearchview

Blinking cursor after text in SearchView without showing soft keyboard


I am trying to imitate the style of how search is handled by the gmail app which looks as follows:

enter image description here

As shown above there is a (blue) blinking cursor after the search query in the searchView.

My respective code looks like this:

SearchReslutActivity.class

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQuery(getIntent().getStringExtra(SearchManager.QUERY), false);
    searchView.setIconified(false);
    searchView.clearFocus();

    return true;
}

menu/search_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/search"
    android:title="@string/search_title"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always" /> </menu>

xml/searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/hint"
android:includeInGlobalSearch="true"
android:inputType="textCapWords"
android:label="@string/app_name"
android:searchSuggestAuthority="com.webaddress.myproject.data.Provider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://com.webaddress.myproject.data.Provider/table"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="1" />

The above code achieves the same look as what is displayed in the gmail app except for having a blinking cursor. How can I add such a blinking cursor without requesting focus on the searchView (which in turn will make the soft keyboard pop up again)?

Thanks!


Solution

  • Thanks to @Azizbekian's answer (which did not work on its own for me) I finally got it working with the following code (in an empty project - support libs 25.3.0):

    java/MainActivity

    package com.yourwebpage.yourapplication;
    
    import android.app.SearchManager;
    import android.os.Bundle;
    import android.support.v4.view.MenuItemCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.SearchView;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(final Menu menu) {
            getMenuInflater().inflate(R.menu.search_menu, menu);
            return true;
        }
    
        @Override
        public boolean onPrepareOptionsMenu(final Menu menu) {
            MenuItem menuItem = menu.findItem(R.id.search);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
            searchView.setQuery(getIntent().getStringExtra(SearchManager.QUERY), false);
            searchView.setIconified(false);
            SearchView.SearchAutoComplete searchAutoComplete =
                    (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
            searchAutoComplete.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(final View v, final boolean hasFocus) {
                    v.requestFocus();
                }
            });
            searchView.clearFocus();
            return super.onPrepareOptionsMenu(menu);
        }
    }
    

    res/layout/activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
        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="com.yourwebpage.yourapplication.MainActivity">
    </android.support.constraint.ConstraintLayout>
    

    res/menu/search_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/search"
            android:title="Search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="always"/>
    </menu>
    

    manifests/AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.yourwebpage.yourapplication">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity"
                android:windowSoftInputMode="stateAlwaysHidden">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    I am posting this rather comprehensive solution in the hope of making it reproducible thereby.