7

I know that this question has been posted million of time, but I can't get a working solution for my case.

I need to change the cursor color of my searchView. I don't have a xml definition of the searchView, I use it programmatically.

Here my code:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_contacts, menu);

    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
    search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

    return true;
}

I'm using this: import android.support.v7.widget.SearchView;

Thanks

Manfredi
  • 172
  • 1
  • 12
  • 2
    If you're using the latest AppCompat library, I think you can change the cursor color by changing the **colorAccent** value in your theme. – David Heisnam Aug 29 '15 at 16:06

2 Answers2

39

Did you try it?

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_contacts, menu);

    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
    search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

    AutoCompleteTextView searchTextView = (AutoCompleteTextView) search.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, R.drawable.cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {
    }
    return super.onCreateOptionsMenu(menu);
}


drawable/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="#ffffff" />
    <size android:width="2dp" />
</shape>

For example: Changing the cursor color in SearchView without ActionBarSherlock

Community
  • 1
  • 1
kolombo
  • 1,091
  • 9
  • 14
4

Another way changing cursor color could be done by style.

Fist create cursor.xml drawable file

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />   //your desired cursor color
<size android:width="2dp" />
</shape>

In style/theme res folder define a style

<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/cursor</item>

and add the below line in your related Activity style or AppTheme

 <item name="autoCompleteTextViewStyle">@style/cursorColor</item>
Ahmet B.
  • 1,290
  • 10
  • 20