Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom search bar with rounded corner xamarin forms android

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.

like image 357
Riyas Avatar asked Dec 18 '22 03:12

Riyas


1 Answers

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>
like image 150
SushiHangover Avatar answered Jan 18 '23 23:01

SushiHangover