4

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?

JackPot16
  • 97
  • 1
  • 1
  • 10

2 Answers2

9

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?

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • In UWP it shows a white background around the roundedEditor. Do you have any idea how can I remove it? – JackPot16 Jul 10 '17 at 10:48
  • I can't reproduce the issue on my phone, I'm using Lumia 640, 15063. Could you please check if it is your custom Editor's layout background color that causing this? – Elvis Xia - MSFT Jul 11 '17 at 01:25
  • I added pictures to explain the problem better in the Edit. The background color I used in Editor is transparent – JackPot16 Jul 16 '17 at 06:11
  • In addition, In android I get `Java.Lang.NoSuchMethodError: no method with name='getDrawable' signature='(I)Landroid/graphics/drawable/Drawable;' in class Landroid/content/Context;"` – JackPot16 Jul 16 '17 at 08:04
  • Please try run my complete demo to check if the problem persists. And this background issue isn't reproducable in Windows 15063, so please try update to the latest windows SDK. Please check my updates. – Elvis Xia - MSFT Jul 17 '17 at 00:42
  • The background issue is resolved when I use the code from your demo. The android error remains. It might be because I run on too old android version?(Android 4.4) – JackPot16 Jul 17 '17 at 05:44
  • 1
    Yes, this API was introduced in API level 21. So for Android 4.4, you need to use `Resources.GetDrawable(Resource.Drawable.RoundedEditText);` instead of `Xamarin.Forms.Forms.Context.GetDrawable`. – Elvis Xia - MSFT Jul 17 '17 at 08:41
6

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!

Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46
  • 1
    Nice simple solution. For clarity, you need to set Padding=0 on your Frame, and the CornerRadius property for this to work. E.g ```Frame IsClippedToBounds="true" Padding="0" CornerRadius="25"> ``` – Adam Diament Aug 21 '19 at 18:19
  • 1
    @AdamDiament good point, I updated it. – Le Mot Juiced Oct 06 '21 at 19:29