5

enter image description here Hello. I am trying to change SearchView text input cursor color and I'm facing a lot of difficulties. I've found that it depends on my colorAccent value defined in AppTheme, but I can't change it, because it will affect a lot of other UI elements. When I try to set custom style to my Toolbar with own colorAccent parameter, nothing changes, the cursor is still the same color (very close to the Toolbar one, so it seems to be invisible).

My Toolbar

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/newsfeed_toolbar"
        android:layout_width="match_parent"
        android:visibility="visible"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        style="@style/SearchStyle"
        app:titleTextAppearance="@style/toolbar_title_style"
        popupTheme="@style/AppTheme.PopupOverlay" >

    </android.support.v7.widget.Toolbar>

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

My search menu

   <?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/newsfeed_search"
    android:icon="@mipmap/ic_search"
    app:showAsAction="always|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>
    </menu>

My style

<style name="SearchStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar"> <item name="android:textCursorDrawable">@android:color/white</item> <item name="android:colorAccent">@android:color/white</item> </style>

Activity code

toolbar = (Toolbar) findViewById(R.id.newsfeed_toolbar);
setSupportActionBar(toolbar);
 @Override
public boolean onCreateOptionsMenu( Menu menu) {
    getMenuInflater().inflate( R.menu.menu, menu);

    MenuItem searchItem = menu.findItem( R.id.newsfeed_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    \\The next few lines is what I tried from other StackOverFlow responses, but no success    
final int textViewID = searchView.getContext().getResources().getIdentifier("android:id/search_src_text",null, null);
    final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(textViewID);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, android.R.color.white); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {}
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchWord = query;
            \\API Stuff

            return false;
        }
        @Override
        public boolean onQueryTextChange(String s) {
            if (s.length()>5) {
                \\API Stuff
            return false;
        }
    });
    MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            searchMode = true;
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            searchMode = false;
            searchWord = null;
            pageCount = 1;
            return true;
        }
    });
    return true;
}
inthy
  • 362
  • 6
  • 16

4 Answers4

19

I have same problem but i finally found by using style

add this item style to your Activity Theme styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="autoCompleteTextViewStyle">@style/cursorColor</item>
    </style>

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

Then add cursor.xml in drawable folder

    <?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="1dp" />
</shape>

And remove search style whatever you are setting in Toolbar.

Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
2
**Try this**

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>
  • Welcome to Stack Overflow! Please don't just throw your source code here. Be nice and try to give a nice description to your answer, so that others will like it and upvote it. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Jun 07 '18 at 07:26
0

When I try to set custom style to my Toolbar with own colorAccent parameter, nothing changes

I see you have a typo when you set style to the toolbar

 <android.support.v7.widget.Toolbar
    android:id="@+id/newsfeed_toolbar"
    android:layout_width="match_parent"
    android:visibility="visible"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    style="@style/SearcStyle"
    app:titleTextAppearance="@style/toolbar_title_style"
    popupTheme="@style/AppTheme.PopupOverlay" >

SearcStyle

Could that be the case?

xklakoux
  • 697
  • 5
  • 12
  • thank you for your attention, this mistake wasn't present in my code, I made it when I was renaming styles and some variables for publishing here – inthy Sep 16 '16 at 11:48
0

This is component of android. The implementation is different.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:autoCompleteTextViewStyle">@style/searchViwStyle</item>
</style>

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

And drawable/color_cursor.

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
   <size android:width="2dp" />
   <solid android:color="@color/cursor_color"  />
</shape>
Robson Chico
  • 121
  • 1
  • 3