Sanyasirao Mopada
Sanyasirao Mopada

Reputation: 953

android search view cursor not visible as same color of toolbar

    <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

Upvotes: 4

Views: 5777

Answers (2)

Parama Sudha
Parama Sudha

Reputation: 2623

Use colorAccent in style.xml...

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/orange</item>
    <item name="colorPrimaryDark">@color/dark_orange</item>
    <item name="colorAccent">@color/blue</item>
</style>

Upvotes: 4

Mukesh Rana
Mukesh Rana

Reputation: 4091

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..!!

Upvotes: 13

Related Questions