4

Custom searchBar renderer with Rounded corner : iOS

 protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
            {
                base.OnElementChanged(e);

                var searchbar = (UISearchBar)Control;                
                if (e.NewElement != null)
                {                    
                    searchbar.Layer.CornerRadius = 20;
                    searchbar.Layer.BorderWidth =14;
                    searchbar.Layer.BorderColor =  UIColor.FromRGB(240,240,240).CGColor;
                }
            }

Custom SearchBar renderer : Android ?

I need to do the same as well as i did for ios , customized search bar with rounded corner and other few customizations , for android i didnt get sufficient informations to fix this. Anybody give some instrutions or ideas .

Thanks in advance.

Rob
  • 2,243
  • 4
  • 29
  • 40
Riyas
  • 475
  • 1
  • 8
  • 27

2 Answers2

7

In your SearchBarRenderer subclass assign the Android SearchView a custom shape drawable:

class CustomSearchBar : SearchBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
            Control.Background = ContextCompat.GetDrawable(Forms.Context, Resource.Drawable.custom_search_view);
    }
}

In the drawable, customize it to your requirements:

<?xml version="1.0" encoding="UTF-8" ?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:padding="10dp"
 android:shape="rectangle" >
 <corners android:radius="10dp" /> 
 <solid android:color="#baf4ed" />
  <stroke
    android:width="5.0dp"
    android:color="#800000" />
 </shape>

For more info on drawables, review the Android docs:

Re: https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Two questions, one (1) the view I'm using is c# so how would you implement this in c# code... two (2). Moving forward, should I be using xaml for all the views instead of the already c# views? Is xaml more of a "standard?" – Tim Maxey Mar 19 '18 at 19:58
  • or!!! lol, is this saying create an xml file called "custom_search_view" under resources folder?? thanks! – Tim Maxey Mar 19 '18 at 20:00
  • Did you create the `custom_search_view` in the drawable folder in the android platform project? – Marc-Anthony Apr 28 '19 at 02:46
  • @Anthony Yes, it is a drawable – SushiHangover Apr 28 '19 at 03:01
5

Not too sure of your exact requirements, but how about simply surrounding the searchbar with a frame?

<Frame Padding="0" Margin="0" BackgroundColor="White" HasShadow="False" BorderColor="Black" CornerRadius="15" HeightRequest="30">
   <SearchBar BackgroundColor="Transparent"/>
</Frame>

searchbar inside trimmed frame image

t.shikanai
  • 71
  • 1
  • 1