0

I am trying to make the text the user types of the search view white. "android:searchViewTextField" give an error. I can't find the right name for a global style:

<style name="AppTheme" parent="@style/_AppTheme"/>

<style name="_AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
  <item name="android:searchViewTextField">@color/white_color</item>
 </style>

Is there a global style which will only affect the text of the Searchview and not all text boxes?

Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • Check this out: https://stackoverflow.com/questions/18259707/change-appcompats-searchview-text-and-hint-color/66246372#66246372 – KennyAli Feb 17 '21 at 16:49

4 Answers4

1

Define the theme "SearchTextViewTheme"

<style name="SearchTextViewTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:textColor">@color/white_color</item>
</style>

Then inside the TextView

<TextView
    style="@style/SearchTextViewTheme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

In fact applying theme must be explicitly defined in the layout XML. Therefore you do not have to worry about the theme affecting other text boxes

Patrick Chan
  • 1,019
  • 10
  • 14
1

If your targed SDK is 20 or less, the attributes Goolge uses to style the SearchView are hidden and you’ll end up with compilation errors if you try overriding them in your theme.

You can use the AppCompat v20 SearchView instead of the the native SearchView following this tutorial. AppCompat exposes attributes like searchViewTextField and searchViewAutoCompleteTextView to change the style of the AutoCompleteTextView.

rubenlop88
  • 4,171
  • 2
  • 28
  • 32
0

Here's how it's done ins Xamarin (I based the idea from Patrick's answer):

     [assembly: ExportRenderer(typeof (CustomSearchBar), typeof (CustomSearchBarRenderer))]

      namespace Bahai.Android.Renderers
      {
      public class CustomSearchBarRenderer : SearchBarRenderer
     {
       protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            SearchBar element = (SearchBar) this.Element;
            var native = (global::Android.Widget.SearchView) Control;

            // do whatever you want to the controls here!
            //--------------------------------------------
            // element.BackgroundColor = Color.Transparent;
            // native.SetBackgroundColor(element.BackgroundColor.ToAndroid());
            // native.SetBackgroundColor(Color.White.ToAndroid());

            //The text color of the SearchBar / SearchView 
            AutoCompleteTextView textField = (AutoCompleteTextView)
                (((Control.GetChildAt(0) as ViewGroup)
                    .GetChildAt(2) as ViewGroup)
                    .GetChildAt(1) as ViewGroup)
                    .GetChildAt(0);

            if (textField != null)
                textField.SetTextColor(Color.White.ToAndroid());
        }
    }
  }
  }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
0

this did the trick

 <style name="myTheme" parent="@style/Theme.AppCompat.Light">    
    <item name="android:editTextColor">#fffff</item>
  </style>

The above solutions might work in java but they don't work in xamarin and i guess this solution will work in java.

megaKertz
  • 484
  • 6
  • 11