简体   繁体   中英

Android SearchView - change cursor color

I am trying to change the cursor color of my SearchView which I instantiate and add in my action bar like this:

MenuItem findItem = menu.findItem(R.id.menu_action_search);
        searchView = (SearchView) findItem.getActionView();

Xml code:

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

      <item android:id="@+id/menu_action_search"
          android:title="Search"
          android:icon="@drawable/ic_search_white"
          app:showAsAction="collapseActionView|always"
          app:actionViewClass="android.support.v7.widget.SearchView" />

</menu>

And my activity has a Toolbar like this:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

            style="@style/EActionBar"

            />
</android.support.design.widget.AppBarLayout>

Is there a way to override an existing style and change the cursor color of the autocomplete text view?

You can do the following steps to change cursor color.

Step 1: Define your own cursor drawable which placed in res/drawable folder.

my_cursor.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#0f0"/>
    <size android:width="1dp"/>
</shape>

Step 2: Add this code into onCreateOptionsMenu .

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

    MenuItem findItem = menu.findItem(R.id.menu_action_search);
    searchView = (SearchView) findItem.getActionView();

    // Add this code
    SearchView.SearchAutoComplete searchTextView = searchView.findViewById(R.id.search_src_text);
    try {
        Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
        field.setAccessible(true);
        field.set(searchTextView, R.drawable.my_cursor);
    } catch (Exception e) {
        // Ignore exception
    }

    // Your logic code here

    return super.onCreateOptionsMenu(menu);
}

From Android Q and up you can do this easily in code. First, find your searchView:

SearchView search = findViewById(R.id.search);

Then find the searchText field using your searchView:

EditText searchText = search.findViewById(androidx.appcompat.R.id.search_src_text);

Then use this to change the color of the cursor:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    searchText.setTextCursorDrawable(R.drawable.cursor_primary);
}

You also need to create a drawable for each color you wish to use. In my case above it's cursor_primary.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary"/>
    <size android:width="1dp"/>
</shape>

That's all!

Update 8th of July 2022

I later discovered that the drawable isn't properly updated until restarting the app. So, instead of using drawables, use this:

searchText.getTextCursorDrawable().setTint(ContextCompat.getColor(context, R.color.colorPrimary));

..and that one works as it should!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM