Edward Yang
Edward Yang

Reputation: 11

How to remove the cancel button of SearchBar in Android?

I am making an Android app. I am using .NET Maui for my app. I have a SearchBar in my app. I tried to use custom control to modify the SearchBar. However, I failed to do so. How do I use custom control to modify the SearchBar?

Any advice or suggestion is welcome.

Upvotes: 0

Views: 487

Answers (2)

    Microsoft.Maui.Handlers.SearchBarHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
    {
#if ANDROID
        var close_btn_view = (handler.PlatformView as AndroidX.AppCompat.Widget.SearchView).FindViewById(Resource.Id.search_close_btn);
        ((Android.Views.ViewGroup)close_btn_view.Parent).RemoveView(close_btn_view);
#endif
    });

Upvotes: 0

Jianwei Sun - MSFT
Jianwei Sun - MSFT

Reputation: 4332

You can use Handler to achieve it:

void ModifySearchBar()
    {
        Microsoft.Maui.Handlers.SearchBarHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
        {
#if ANDROID
            var close_btn_view = (handler.PlatformView as SearchView).FindViewById(Resource.Id.search_close_btn);
            ((ViewGroup)close_btn_view.Parent).RemoveView(close_btn_view);
#endif
        });
    }

For more info, you can see my pervious answer: For android, there is space in cancel button from right side. how can i remove that?

Upvotes: 0

Related Questions