Moiz Mahmood
Moiz Mahmood

Reputation: 1

How can i give dynamic padding to an Entry in Xamarin forms?

How can i give dynamic padding to an Entry in Xamarin forms which looks same on every mobile screen size? I am trying to apply padding in this way using EntryRenderer.

Control.SetPadding(20,20,0,0);

On small screen sizes it looks perfect but on large screen sizes it acts different and looks pretty odd.

Upvotes: 0

Views: 859

Answers (2)

Andrew
Andrew

Reputation: 1438

The Xamarin.Essentials nuget has a feature for getting device display information.

When using the Xamarin.Essentials nuget, make sure to follow their getting started steps.

You could use it to get information about the screen like:

var screenWidth   = DeviceDisplay.ScreenMetrics.Width; // in pixels
var screenHeight  = DeviceDisplay.ScreenMetrics.Height: // in pixels
var screenDensity = DeviceDisplay.ScreenMetrics.Density;

And then use some combination of that information to set your padding:

if(screenWidth <= 400)
{
    Control.SetPadding(20,20,0,0);
}
else if(screenWidth > 400 && screenWidth < 500)
{
    Control.SetPadding(30,20,0,0);
}
else if // continue with whatever conditions need checked

Note that I used screenWidth in my example, but screenDensity may be better suited for your problem. You may have to adjust a few times until you find what works.

Upvotes: 2

Aritra Das
Aritra Das

Reputation: 272

If you already have a custom Entry where you are able to set the padding, then it's pretty simple.

        if (Xamarin.Forms.Device.Idiom == TargetIdiom.Phone)
        {
            Control.SetPadding(20,20,0,0);
        }
        else if(Xamarin.Forms.Device.Idiom == TargetIdiom.Tablet)
        {
            Control.SetPadding(40,40,0,0);
        }

You can do like this.

Upvotes: 0

Related Questions