Search code examples
androidxamarinsearchview

Get the textview inside of a SearchView with Xamarin for Android


I'm currently developing an Android app using Xamarin.Android. In this app, I have a SearchView that I made smaller than the standard size. So, when I type in it, the text is too big to be fully shown inside the bar. I searched for this and found this link :

How to remove inner bottom space (bottom margin/padding) of text/layout inside a SearchView?

The person that made this post is looking for the exact same result than me, except he's not using Xamarin. I found similar posts, all for Android, with the same answer that seems to work.

I'm looking for the Xamarin equivalent to that answer :

AutoCompleteTextView searchTextContent = (AutoCompleteTextView) searchBar.findViewById(searchBar.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));

As getContext(), getResources(), etc aren't existing in Xamarin, but probably have an equivalent way to get this AutoCompleteTextView.

Thanks in advance !


Solution

  • Here: This should help you

    you can use

    Application.Context 
    

    or more specifically,

    Android.App.Application.Context
    

    instead of getContext()

    for your getResources() make it GetResources()

    So this is the solution:

    AutoCompleteTextView searchQuery = searchBar.FindViewById<AutoCompleteTextView>(Application.Context.Resources.GetId‌​entifier("android:id/search_src_text", null, null)); 
    searchQuery.TextSize = 15;
    searchQuery.Gravity = GravityFlags.Bottom;
    

    Happy Coding