Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Rounded Editor control in Xamarin Forms

I'm making a cross platform app(Android, WinPhone) using xamarin forms. I need to create a Rounded Textbox, just like the input box in Whatsapp chat window. The textbox control is called Editor in Xamarin Forms.

Does anyone know how to create a rounded corner editor? I've tried implementing a renderer in both platforms but didn't find what I was looking for.

Edit

After trying your method the Editor looks like this when unclicked: enter image description here

And looks like this when clicked:

enter image description here

The background shape is rectangle for some reason, I'd prefer if it will be only in the borders of the editor. Any ideas how?

like image 290
JackPot16 Avatar asked Dec 07 '22 17:12

JackPot16


2 Answers

Does anyone know how to create a rounded corner editor? I've tried implementing a renderer in both platforms but didn't find what I was looking for.

Your direction is correct. You need to create custom render for each platform. And please follow the following steps to create a rounded Editor in both platforms:

  1. Custom Control RoundedEditor in PCL:

    public class RoundedEditor:Editor
    {
      //empty or define your custom fields
    }
    

For Android Platform(in YourProject.Android):

  1. Create an xml RoundedEditText.xml in Resources/Drawable/:

    <?xml version="1.0" encoding="utf-8" ?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp" >
      <!--solid defines the fill color of the Editor-->
      <solid android:color="#FFFFFF"/>
      <!--stroke defines the border color-->
      <stroke android:width="2dp" android:color="#FF0000" />
      <!--the corner radius-->
      <corners
       android:bottomRightRadius="15dp"
       android:bottomLeftRadius="15dp"
       android:topLeftRadius="15dp"
       android:topRightRadius="15dp"/>
    </shape>
    
  2. Create your Custom Renderer like this:

    [assembly:ExportRenderer(typeof(RoundedEditor),
    typeof(RoundedEditorRenderer))]
    namespace RoundedEditorDemo.Droid
    {
        public class RoundedEditorRenderer:EditorRenderer
        {
    
            protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    Control.Background = Xamarin.Forms.Forms.Context.GetDrawable(Resource.Drawable.RoundedEditText);
                }
            }
        }
    }
    

For Windows Platform (in YourProject.UWP):

  1. Create a ResourceDictionary file by right click your project->Add->New Item->Resource Dictionary->rename to RoundedEditorRes.xaml and add the full TextBox default style to the resource dictionary.

  2. Edit the Border element(add CornerRadius="15" and change BorderThickness="{TemplateBinding BorderThickness}" to BorderThickness="2" ) of the TextBox's default style and add a key to the style:

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:RoundedEditorDemo.UWP">
        ...
        <Border 
             CornerRadius="15"
             BorderThickness="2"
             x:Name="BorderElement" 
             BorderBrush="{TemplateBinding BorderBrush}" 
             Background="{TemplateBinding Background}" 
             Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="1"/>
        ...
    </ResourceDictionary>
    
  3. Create your Custom Renderer:

    [assembly: ExportRenderer(typeof(RoundedEditor), 
    typeof(RoundedEditorRenderer))]
    namespace RoundedEditorDemo.UWP
    {
        public class RoundedEditorRenderer:EditorRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    Windows.UI.Xaml.ResourceDictionary dic = new Windows.UI.Xaml.ResourceDictionary();
                    dic.Source = new Uri("ms-appx:///RoundedEditorRes.xaml");
                    Control.Style = dic["RoundedEditorStyle"] as Windows.UI.Xaml.Style;
                }
            }
        }
    }
    

And Here is the Complete Demo:RoundedEditorDemo.

Update:

I can't reproduce the background issue, I'm using Windows update 15063. So I think it will be fixed if you update to the latest update.

enter image description here

enter image description here

For Android part:Please notice that I'm using Xamarin.Forms.Forms.Context.GetDrawable, it is provided by Xamarin.Forms. And please try run my complete demo on your computer to check if you get the error.

If you still get the error. Could you please point out, in which did you get the error?

like image 155
Elvis Xia - MSFT Avatar answered Dec 27 '22 20:12

Elvis Xia - MSFT


Unless I'm missing something, you don't need custom renderers or anything like that. All you need is a frame.

Here's how I'm doing it:

<Grid>
     <Frame IsClippedToBounds="true"
            Padding="0"
            CornerRadius="25">
          <Editor />
     </Frame>
</Grid>

Working for me!

like image 44
Le Mot Juiced Avatar answered Dec 27 '22 21:12

Le Mot Juiced