4

I want to change the search text view color in Xamarin.Android. I have tried below code so far

        searchView = this.Activity.FindViewById<Android.Support.V7.Widget.SearchView (Resource.Id.searchView);
        searchView.SetOnQueryTextListener(this);
        var textViewId = searchView.Context.Resources.GetIdentifier("android:id/search_src_text", null, null);
        var textView = (searchView.FindViewById(textViewId) as TextView);
        if (textView != null)
            textView.SetTextColor(global::Android.Graphics.Color.White);

I am getting NULL when I try to capture the textView

In textViewId I'm getting the id of the view something like 126312727

Can anyone help me on above?

SandeepM
  • 2,601
  • 1
  • 22
  • 32
  • Check this out: https://stackoverflow.com/questions/18259707/change-appcompats-searchview-text-and-hint-color/66246372#66246372 – KennyAli Feb 17 '21 at 16:50

2 Answers2

2

This is right out of app using Android.Support.V7.Widget.SearchView

var id = searchView.Context.Resources.GetIdentifier("search_src_text", "id", PackageName);
var searchEditText = searchView.FindViewById<EditText>(id);
searchEditText.SetTextColor(Color.Red);
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks!! Its working, but the cursor is having the theme color. How can we change the cursor color ? – SandeepM Jul 11 '18 at 08:49
  • Hey @SushiHangover, Is it possible to change the cursor color too? I've tried to change the accent color but it was defined in the global theme so how to do that just for the SearchView? – SandeepM Jul 11 '18 at 13:43
  • @Sandeep-Systematix yes, but do not remember off the top of my head... i'll look at some of my code later... – SushiHangover Jul 11 '18 at 20:28
0

I needed to use the below with AppCompat and CrossCurrentActivity plugin.

var searchEditText = (CrossCurrentActivity.Current?.Activity as MainActivity)?.FindViewById<AutoCompleteTextView>(Resource.Id.search_src_text);
searchEditText.SetTextColor(Color.Red);
Jonty
  • 662
  • 7
  • 8