I want to change the "SearchBar" element to suit myself (Android)

валера карманов 396 Reputation points
Oct 11, 2024, 5:55 AM

I use a class inherited from "EntryHandler" to remove the background of the "SearchBar" element and it is removed, but an exception occurs when entering into the "SearchBar" text field.

System.InvalidCastException: 'Specified cast is not valid.'

Perhaps I am not inheriting from the right class, which is why the error occurs.

By the way, for the "Entry" element everything works without errors

handlers.AddHandler(typeof(SearchBar), typeof(CustomEntryHandler));

public partial class CustomEntryHandler : EntryHandler
{	
	protected override void ConnectHandler(AppCompatEditText platformView)
	{
		#if ANDROID
			platformView.Background = null;
		#endif
	}
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,797 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 46,586 Reputation points Microsoft Vendor
    Oct 11, 2024, 8:12 AM

    Hello,

    System.InvalidCastException: 'Specified cast is not valid.'

    This error is usually caused when the wrong type conversion is used.

    This is an expected error, SearchBar should use SearchBarHandler, not EntryHandler.

    Please refer to the following code sample and documentation:

    namespace MauiApp12.Platforms.Droid
    {
        public class CustomerSearchBarHandler:SearchBarHandler
        {
            protected override void ConnectHandler(SearchView platformView)
            {
                // this code sample shows how to remove the underline of searchbar.
                base.ConnectHandler(platformView);
                Android.Widget.LinearLayout linearLayout = platformView.GetChildAt(0) as Android.Widget.LinearLayout;
                linearLayout = linearLayout.GetChildAt(2) as Android.Widget.LinearLayout;
                linearLayout = linearLayout.GetChildAt(1) as Android.Widget.LinearLayout;
                linearLayout.Background = null;
            }
        }
    }
    
    //register handler
    .ConfigureMauiHandlers(handlers =>
                    {
    #if ANDROID
                        handlers.AddHandler(typeof(SearchBar), typeof(CustomerSearchBarHandler));
    #endif
                    })
    

    You could see the view handlers in View handlers.

    Update:

    You could refer to the following code to remove the magnifying glass and clear button in SearchBar.

    protected override void ConnectHandler(SearchView platformView)
    {
        base.ConnectHandler(platformView);
        var search = platformView as SearchView;
        ImageView searchViewIcon = (ImageView)search.FindViewById<ImageView>(Resource.Id.search_mag_icon);
        searchViewIcon.SetImageDrawable(null);
     
        ImageView closeViewIcon = (ImageView)search.FindViewById(Resource.Id.search_close_btn);
        closeViewIcon.SetImageDrawable(null);
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.