question

WeiWen-3421 avatar image
0 Votes"
WeiWen-3421 asked WeiWen-3421 commented

Why my Custom Entry does not appear to be any different than the built-in Entry

I need to remove the top and bottom padding of the built-in Entry, because I need the entry's height to be small. If there is too much padding, the text will not appear. So I implemented a custom entry with top and bottom padding set to 0. But it still looks like the built-in Entry. Text still does not appear. Only after I increase the Entry's height large enough, will the text appear.

Here is my custom renderer for Android:


[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace MyApp.Droid.CustomControls
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}

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

   if (e.NewElement == null)
   {
     return;
   }

   if (this.Element is CustomEntry customEntry)
   {
     var paddingLeft = (int)customEntry.Padding.Left;
     var paddingRight = (int)customEntry.Padding.Right;
     this.Control.SetPadding(paddingLeft, 0, paddingRight, 0);
   }
 }

}
}




dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered WeiWen-3421 commented

Hello,​

Welcome to our Microsoft Q&A platform!

I created a basic demo to test the function and it works as expected (I removed the underline for the custom entry).

<Entry Text="testing for padding" BackgroundColor="LightCyan" HeightRequest="25"/>
<local:CustomEntry Text="testing for padding" BackgroundColor="LightBlue" HeightRequest="25"/>

105938-image.png

What is the value of the HeightRequest you set for the entries? The default value of the fontsize is 18, the content could not be shown completely if the HeightRequest's value is too small.

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

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.




image.png (4.3 KiB)
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JarvanZhang-MSFT It appeared to be working when I tested it again. Maybe Visual Studio didn't updated the first time. My HeightRequest was set to 20, and fantasize was 10. Do you know how to implement the renderer with the same behavior for iOS? Thanks!

0 Votes 0 ·

@WeiWen-3421 In iOS, the entry doesn't have the top and bottom space like Android platform. You could just specify a value to the HeightRequest and FontSize directly.

<local:CustomEntry Text="testing for padding" BackgroundColor="LightBlue" HeightRequest="20" FontSize="16"/>
1 Vote 1 ·

@JarvanZhang-MSFT The padding on the Android custom entry still is not removed. The other day I said it was is because I ran on iPhone simulator and thought it was Android phone simulator. Now that I look more closely on this, I found that the custom renderer was never called. I don't think the namespace for it is wrong. Otherwise, it wouldn't even pass build. Do you know what could cause the renderer to not be called?

0 Votes 0 ·

Hi, WeiWen. Please make sure the render reigster on Android platform is correct. Here is the related tutorial, please check it.

0 Votes 0 ·

@JarvanZhang-MSFT I did used your link to help me create my custom entry. I think the issue could only be the Assembly declaration in the custom renderer. Your link used this: [assembly: ExportRenderer(typeof(CustomRenderer.CameraPreview), typeof(CameraPreviewRenderer))], which has the fully qualified CameraPreview. Mine already used "using ...", so I don't need to add the full path for CustomEntry. Is there any other reason for the renderer method not called?

0 Votes 0 ·
Show more comments