How to edit icon of SearchBar Xamarin Forms on Android ?

RoexRoex FRMember ✭✭

Hello everyone ! I need your help !
I want to make a page with a SearchBar, but, my problem is the personalisation of the SearchBar icon, especially on the Platform Android. My search led me to nothing.
I would be very grateful if you know of a way to edit icon. (I have a model to follow, so It’s well the icon that I want to change and not the color).
Thank you in advance !

Answers

  • AmishiAmishi INMember

    Hi,
    Were you able to achieve this? I need to do a similar change.
    Thanks!

  • jordydieltjensjordydieltjens USMember
    edited April 2017

    you need a renderer to achieve this :
    this is my code, it changes the search and the cancel icon
    `[assembly: ExportRenderer(typeof(CustomSearchBar), typeof(CustomSearchbarRenderer))]

    namespace namespace.Droid.Renderer
    {
    public class CustomSearchbarRenderer : SearchBarRenderer
    {
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);
    if (Control != null)
    {
    var searchView = Control;
    searchView.Iconified = true;
    searchView.SetIconifiedByDefault(false);
    // (Resource.Id.search_mag_icon); is wrong / Xammie bug
    int searchIconId = Context.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
    var icon = searchView.FindViewById(searchIconId);

                (icon as ImageView).SetImageResource(Resource.Drawable.youriconforsearch);
    
                int cancelIconId = Context.Resources.GetIdentifier("android:id/search_close_btn", null, null);
                var eicon = searchView.FindViewById(cancelIconId);
                (eicon as ImageView).SetImageResource(Resource.Drawable.youriconforcancel);
            }
        }
    }
    

    }`

  • LeoJHarrisLeoJHarris NZMember ✭✭✭

    @jordydieltjens Dont forget to add the TElement

    protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)

  • DimChrisDimChris USMember ✭✭✭

    Why dont you put a transparent button(with your png image inside) side by side your editext ?

Sign In or Register to comment.