How to Change the Textcolor on an Android Searchview

How to change the textcolor or queryhint textcolor of an Android SearchView in AndroidX?

The searchView below is the reference variable of Searchview you are using .

JAVA:

    EditText editText = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
editText.setTextColor(Color.WHITE);
editText.setHintTextColor(Color.GRAY);

KOTLIN

val editText = searchView.findViewById(androidx.appcompat.R.id.search_src_text)
editText.setTextColor(Color.WHITE)

SearchView text color is not changing

Found a solution. Create a new theme with the parent Widget.AppCompat.SearchView and then you can customize it's font, text size, text color etc.

Code;

<style name="SearchViewTheme" parent="Widget.AppCompat.SearchView">
<item name="android:textColorHint">@color/somecolor</item>
<item name="editTextColor">@color/somecolor</item>
<item name="fontFamily">@font/somefont</item>
</style>

Set text color and hint text color to the text in SearchView

I would do this kind of job through the xml layout file. This can be found in the res/layout folder.

You would need to do something similar to the following:

Add this to the parent theme.

<item name="android:editTextColor">@android:color/white</item>

This should change the entered text.

You can also use something like this:

<item name="android:textColorHint">@android:color/white</item>

It will change the hint text for the SearchView.

Hope this helps :)

Change TextColor in SearchView using Android Toolbar

After some research I found a secure way to do it.

Digging up in the SearchView styles, I found the layout that is used to display the SearchView. Inside that layout there's a TextView (the actual field where you type in the SearchView)

<TextView
android:id="@+id/search_badge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginBottom="2dip"
android:drawablePadding="0dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary"
android:visibility="gone" />

Notice the field android:textColor="?android:attr/textColorPrimary". This causes the problem I originally had, the text color in the SearchView is the same as the one defined for the Title text color in the Android Toolbar.

Now there're are several solutions that might work here, but I think that most of those solutions have the same problem. They all rely in the id of the TextView in order to access the view and change the text color, as described here

Personally I think that hardcoding the id of the TextView inside the code is highly risky, because we don't know if tomorrow Google decides to use another id value for this view, in that case, our code will be broken.

For that reason I've created a recursive method that obtains the TextView object in the SearchView, and changes the color to whatever we want.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.search, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

searchView.setOnQueryTextListener(new SearchTextListener());
changeSearchViewTextColor(searchView);
}

private void changeSearchViewTextColor(View view) {
if (view != null) {
if (view instanceof TextView) {
((TextView) view).setTextColor(Color.BLACK);
return;
} else if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
changeSearchViewTextColor(viewGroup.getChildAt(i));
}
}
}
}

I've tested this code using API 21 with the Toolbar, having the Toolbar Title text color set to white, and setting up the SearchView text color to black and it works perfectly. Also, as we're accessing the TextView object directly, we can change the hint, drawable, paddings, and everything related to the TextView.



Related Topics



Leave a reply



Submit