0

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?

Andrei F
  • 3,759
  • 6
  • 28
  • 60

1 Answers1

4

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);
}
Son Truong
  • 10,947
  • 5
  • 23
  • 50
  • This method works, but like this: final android.support.v7.widget.SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); ... field.set(searchAutoComplete, R.drawable.white_cursor); So field is needed from the textbox of searchview, not the searchview itself – Andrei F Mar 12 '19 at 07:36
  • @AndreiF Please take a look my answer carefully, I set on textbox of search view already. – Son Truong Mar 12 '19 at 08:03
  • Yes, sorry. I just woke up when I tested the code, didn't even drink my coffee. Thank you! – Andrei F Mar 12 '19 at 09:38
  • No worries. Happy coding :) – Son Truong Mar 12 '19 at 11:23
  • 1
    searchView.findViewById(androidx.appcompat.R.id.search_src_text) - for androidx – Fox Jun 26 '20 at 10:37