Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android search view cursor not visible as same color of toolbar

Tags:

android

search

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right|end">

        <android.support.v7.widget.SearchView
            android:background="@null"
            android:id="@+id/searchView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.Toolbar>

my colorAccent is also colorPrimary, how to change cursor color, I referred other quetions, but nothing solved my problem

like image 848
Sanyasirao Mopada Avatar asked Dec 02 '22 15:12

Sanyasirao Mopada


1 Answers

You have two options.

1). You can make use of these attributes in your styles.xml for changing the cursor color.

<item name="colorControlNormal">@color/colorEditextDisabled</item>
<item name="colorControlActivated">@color/colorEditextEnabled</item> // change this color to the required cursor color your need.
<item name="colorControlHighlight">@color/colorEditextEnabled</item>

2). Find the EditText from your SearchView and set the android:textCursorDrawable attribute to @null. This will result in android:textColor as the cursor color. But as you can't set textCursorDrawable programatically, so you have to make use of reflection in this case.

 EditText etSearch= ((EditText) yourSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
        try {
            Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
            f.setAccessible(true);
            f.set(etSearch, null);// set textCursorDrawable to null
        } catch (Exception e) {
            e.printStackTrace();
        }
        etSearch.setTextColor(ContextCompat.getColor(mContext, R.color.yourColor));

Hopefully, any of the solutions above will work for you..!!

like image 178
Mukesh Rana Avatar answered Dec 05 '22 03:12

Mukesh Rana