0

I have a Xamarin.Forms grid that I'm setting up and I've noticed that the Entry fields are a lot higher (3 - 4 times) than label fields. I can't see any obvious reason why this would be the case.

<Grid RowSpacing="0" ColumnSpacing="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" Text="G/C" FontSize="Small"/>
    <Entry Grid.Row="1" Grid.Column="0" FontSize="Small" BackgroundColor="Red"/>
</Grid>

This results in something like this (on the default Android Emulator, installed 2 days ago); Entry Example

Does anyone have any suggestions as to why the Entry height is so much bigger and what I can do to reduce it (obviously HeightRequest, but I'd rather avoid if possible)?

Trent
  • 1,595
  • 15
  • 37
  • Not enough reputation point to comment , so posting it as an answer. I also faced same issue and i solved it by setting **HeightRequest** property in Entry. – Dinesh Falwadiya Nov 14 '17 at 05:53

2 Answers2

2

Based on your comments here is a suggestion to do what you want in XAML. You can bind Entry.HeightRequest to Label.Height to avoid setting it manually.

<Grid RowSpacing="0" ColumnSpacing="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" Text="G/C" FontSize="Small" x:Name="MyLabel" />
    <Entry Grid.Row="1" Grid.Column="0" FontSize="Small" BackgroundColor="Red" HeightRequest="{Binding Source={x:Reference MyLabel}, Path=Height}"/>
</Grid>
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
  • Of course! Simple and effective. Does exactly what I'm after, though I now realise the text doesn't show because the height is too small, typical! The label needs some padding anyway, I'm sure I can figure it out from here, thanks. – Trent Nov 17 '17 at 08:16
0

Use Star (*) as the height for the rows, this will make all rows have the same height. Auto will set the rows to a height that the control is just fitting in.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
  • So, the Entry control is actually larger than a label? Seems a little odd. I don't think I can use * because this grid is going to be larger than the screen and sit inside a scrollview, if I use * the rows would end up too small wouldn't they? – Trent Nov 14 '17 at 06:09
  • @Trent It depends. When you set the `VerticalOptions` of the grid to `Fill`, I believe that the grid will be compressed to fit the parent, but when using `FillAndExpand` it should trigger the scrolling and take the place needed for its controls. – Paul Kertscher Nov 14 '17 at 06:12
  • Hrmm, using the above definition I added `VerticalOptions="FillAndExpand"` to the Grid tag and set both rows to `*`. Now takes up the entire height of the screen. Just `Fill` also does the same. Thoughts? Maybe I do have to use HeightRequest. I'm just wary about how that will work with different sized screens; I'm intending for this app to work on the majority of devices... – Trent Nov 14 '17 at 14:05
  • @Trent Try with `StartAndExpand` - this should do better. XF layouts are hairy beasts ;) – Paul Kertscher Nov 14 '17 at 14:08
  • 1
    Hah, so I see. We're getting closer; Label is now as big as the Entry... – Trent Nov 15 '17 at 02:15
  • Played around a bit with the VerticalOptions, can't seem to get anywhere. Might have to resort to HeightRequest unless you have any other ideas @Paul Kertscher – Trent Nov 17 '17 at 07:57
  • Sorry, I don't understand what the current issue is. Didn't you write that label and entry have the same height now? – Paul Kertscher Nov 17 '17 at 08:00
  • Ah yeah, sorry. Wrong way around though. The label expanded to the same size as the entry, I'm trying to reduce my space usage. – Trent Nov 17 '17 at 08:01
  • Ah, okay, then I misunderstood the whole issue from the beginning. I just thought that you wanted them the *same* size. – Paul Kertscher Nov 17 '17 at 08:02