Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar SearchView cursor color

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;
}
like image 483
inthy Avatar asked Sep 16 '16 11:09

inthy


1 Answers

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.

like image 125
Mohit Suthar Avatar answered Oct 02 '22 18:10

Mohit Suthar