How do I remove the underline from searchbar in android

Jeff Pfahl 91 Reputation points
2022-09-29T15:23:24.39+00:00

I need to remove the underline from the searchbar in android. The following does not work:

   public partial class SearchBarBase : SearchBar  
   {  
       static SearchBarBase()  
       {  
           SearchBarHandler.Mapper.AppendToMapping(  
               "SearchBarBase",  
               (handler, view) =>  
               {  
                   // TODO   
                   handler.PlatformView.Background = null;  
               });  
       }  
   }  
Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-09-30T05:24:52.69+00:00

    Hello,

    Please do not set directly, if you want to set SearchBar's background to remove the underline, you need get the nested LinearLayout, then set the background to null for nested LinearLayout

       Microsoft.Maui.Handlers.SearchBarHandler.Mapper.AppendToMapping("MyCustomizationSearchBar", (handler, view) =>  
               {  
       #if ANDROID  
                  
                   Android.Widget.LinearLayout linearLayout =  handler.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;  
                   
       #endif  
               });  
           }  
    

    Best Regards,

    Leon Lu


    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.

    5 people found this answer helpful.

0 additional answers

Sort by: Most helpful

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.