Adjust size of label to the wrapped text - css

trying to format 2 labels.
Labels should represent title and value.
They should have position on bottom right corner.
On tablet everything looks fine, but on phone when "value" need to be wrapped. The space where it could not fit the wrapped text on the first line still stay.
So on the end, the text formatting look strange. with empty space where text cannot fit.
In this case I can imagine that size of second label is adjust to the size of longest text on line...
<StackLayout Orientation="Horizontal" HorizontalOptions="End" VerticalOptions="End">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
BackgroundColor="Bisque"
FontSize="Medium"
Text="Version of Co.:"
HorizontalOptions="EndAndExpand"/>
<Label
Grid.Column="1"
BackgroundColor="BlanchedAlmond"
HorizontalOptions="End"
HorizontalTextAlignment="Start"
FontSize="Medium"
Text="This is vereverevery longversiontitle."/>
</Grid>
</StackLayout>
When I will set up horizontalTextAlignment on the second label to "Start" result will look like this and I will have again strange empty space.
With short text which dont need to be wrap, whole layout looks fine as I want:
Update:
Expected behavior:

Just Add one more column to your grid,
and change the position of label to 1, 2 respectively.
<StackLayout Orientation="Horizontal" HorizontalOptions="End" VerticalOptions="End">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="1"
BackgroundColor="Bisque"
FontSize="Medium"
Text="Version of Co.:"
HorizontalOptions="EndAndExpand"/>
<Label
Grid.Column="2"
BackgroundColor="BlanchedAlmond"
HorizontalOptions="End"
HorizontalTextAlignment="Start"
FontSize="Medium"
Text="This is vereverevery longversiontitle."/>
</Grid>
</StackLayout>

Set the outer StackLayout's Orientation as Vertical.
Add the extra column to the Grid, and set the exact width value on it .
Set the Label's HorizontalTextAlignment as End.
Sample code
<StackLayout Orientation="Vertical" HorizontalOptions="End" VerticalOptions="End">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="1"
BackgroundColor="Bisque"
FontSize="Medium"
Text="Version of Co.:"
HorizontalOptions="EndAndExpand"
HorizontalTextAlignment="End"/>
<Label
Grid.Column="2"
BackgroundColor="BlanchedAlmond"
HorizontalOptions="End"
HorizontalTextAlignment="End"
FontSize="Medium"
Text="This is vereverevery longversiontitle."/>
</Grid>
</StackLayout>

Related

How align elements in Xamarin StackLayout?

I am attaching the result of this code.
enter image description here
What I want the layout to look like is that the red stays centered and the yellow goes towards the ends of the Horizontal.
And regardless of the text size of the label inside the yellow, I want the red color to remain centered.
please give me advice
<StackLayout
BackgroundColor="White"
HorizontalOptions="Center"
Orientation="Horizontal">
<Label x:Name="xTitle"
BackgroundColor="Red"
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
Text="asdasdasdas"
VerticalOptions="Fill" />
<Label x:Name="xResult"
BackgroundColor="Goldenrod"
HorizontalOptions="End"
HorizontalTextAlignment="Center"
Text="123123123123123"
VerticalOptions="Fill" />
</StackLayout>
I tried to send the yellow part to the far end while keeping the red label in the center.
You can try to use 'Grid' to achieve this, just as ToolmakerSteve mentioned.
Please refer the following code: 
<StackLayout>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="1"
x:Name="xTitle"
BackgroundColor="Red"
Text="asdasdasdas"
VerticalOptions="Fill"
HorizontalOptions="Center" />
<Label Grid.Row="0"
Grid.Column="2"
x:Name="xResult"
Text="123123123123123"
BackgroundColor="Goldenrod"
HorizontalTextAlignment="Center"
VerticalOptions="Fill"
HorizontalOptions="End"/>
</Grid>
</StackLayout>
For more information, you can check Xamarin.Forms Grid.

Xamarin.Forms stepper is showing only minus button on android

This only shows minus option on the stepper. Why the plus option is not visible? The stepper works just fine on iOS.
<Entry Text="{Binding Path=Quantity, Mode=TwoWay}" Grid.Column="1" Grid.Row="1"></Entry>
<Stepper Value="{Binding Path=Quantity, Mode=TwoWay}"
Grid.Column="2"
Grid.Row="1"
MinimumWidthRequest="200"></Stepper>
I have tried shared code in my Grid , it works .
<Grid VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="400" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Entry Text="{Binding Path=Quantity, Mode=TwoWay}"
Grid.Column="0"
Grid.Row="0"></Entry>
<Stepper Value="{Binding Path=Quantity, Mode=TwoWay}"
Grid.Column="1"
Grid.Row="0"
MinimumWidthRequest="200"></Stepper>
<BoxView Grid.Row="1"
Grid.Column="0"
BackgroundColor="Red" />
<Button Grid.Row="2"
Grid.Column="0"
BackgroundColor="Yellow"
Clicked="Button_Clicked" />
</Grid>
The effects :
If you have a doubt about MinimumWidthRequest , it can be affected by Layout . If using it in StackLayout , then it will work , however in Grid can not .
<StackLayout Orientation="Horizontal" VerticalOptions="Start">
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Entry Text="{Binding Path=Quantity, Mode=TwoWay}" Placeholder="123456789" MinimumWidthRequest="5"
></Entry>
<Stepper Value="{Binding Path=Quantity, Mode=TwoWay}"
MinimumWidthRequest="1"></Stepper>
</StackLayout>
The effects :
In addition , from Remarks of MinimumWidthRequest :
This causes overflow handling to shrink this element to its minimum width before elements who do not have a minimum size set.
I think it just be used to shrink the element , if the element not be supported the limit size , then it will not work .Here Stepper is not recommended for using MinimumWidthRequest. If works, it will cause it to appear asymmetric.

Frame not taking the full height Xamarin forms

I have Xamarin page which has a grid with two rows and a column. In the first row/column I have a label and the second row/column I have a frame where it does not take the full height of the row. Could you help, please?
<controls:GradientColorStack
StartColor="#0cacee"
EndColor="#0574d8"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
GradientOreintation="Vertical">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="330" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Column="0"
Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Text="" />
<controls:CustomFrame
Grid.Row="1"
CornerRadius="30,30,0,0"
BackgroundColor="White"
HasShadow="True"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="12,20,12,0" />
</Grid>
</controls:GradientColorStack>
There is default spacing in Row and Column .
Grid has properties to control spacing between rows and columns. The following properties are available for customizing the Grid:
ColumnSpacing – the amount of space between columns. The default value of this property is 6.
RowSpacing – the amount of space between rows. The default value of this property is 6.
Here you can set RowSpacing= "0" to get full height of the row.
<Grid BackgroundColor="Accent" RowSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="330"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label BackgroundColor="Wheat" Grid.Column="0"
Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Text="1111111111" />
<Frame
Grid.Row="1"
BackgroundColor="White"
HasShadow="True"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="12,0,12,0" />
</Grid>
By the way Margin="12,20,12,0" also can affect the top space, if not necessary ,can set this : Margin="12,0,12,0"
Works fine with frames ?
<Frame
BackgroundColor="Red"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="370" />
</Grid.RowDefinitions>
<Label Grid.Column="0"
BackgroundColor="GREEN"
Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="Start"
Text="LOL" />
<Frame
Grid.Row="1"
Grid.Column="0"
BackgroundColor="White"
HasShadow="True"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="12,20,12,0">
<StackLayout>
<Label Text="Text" />
</StackLayout>
</Frame>
</Grid>
</Frame>

Xamarin Forms: Not able to create a corner radius for a two column layout

I'm trying to create a grid layout with a height of 8px with cornered radius horizontally as below snapshot.
: https://ibb.co/s1dkpnw
<Grid Margin="0,0,0,24" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*">
</ColumnDefinition>
<ColumnDefinition Width="4*">
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="8">
</RowDefinition>
</Grid.RowDefinitions>
<Frame CornerRadius="8" Grid.Row="0" HorizontalOptions="FillAndExpand" HeightRequest="8">
<Label Grid.Row="0" Grid.Column="0" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Lime"></Label>
<Label Grid.Row="0" Grid.Column="1" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Blue"></Label>
</Frame>
</Grid>
I'm able to get the UI as in the below snapshot.
: https://ibb.co/7nLTgdL
I'm not able to get the UI displyed within the frame visible, although i'm getting a cornered radius for a two columned layout. It just shows a empty frame.
Please let me know how to design the XAML UI to get the UI as in the design snapshot.
Got the UI aligned by pushing the Grid within the frame control as below,
<Frame CornerRadius="8" Grid.Row="0" HorizontalOptions="FillAndExpand" HeightRequest="8">
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*">
</ColumnDefinition>
<ColumnDefinition Width="4*">
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="8">
</RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Lime"></Label>
<Label Grid.Row="0" Grid.Column="1" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Blue"></Label>
</Grid>
</Frame>
AbsoluteLayout can help you:
<AbsoluteLayout HorizontalOptions="Center">
<Frame
AbsoluteLayout.LayoutBounds="0,0,150,8"
AbsoluteLayout.LayoutFlags="None"
BackgroundColor="#0086C9"
CornerRadius="7" />
<Frame
AbsoluteLayout.LayoutBounds="120,0,150,8"
AbsoluteLayout.LayoutFlags="None"
BackgroundColor="#D8D8D8"
CornerRadius="7" />
<Frame
AbsoluteLayout.LayoutBounds="120,0,30,8"
AbsoluteLayout.LayoutFlags="None"
BackgroundColor="#0086C9"
CornerRadius="1" />
</AbsoluteLayout>
Output: https://i.ibb.co/jh6QmBL/ss.png

How to setup Row/Column Definitions in Xamarin.Forms Xaml?

I managed to setup rows and columns from the code but couldn't move this settings to xaml:
grid.RowDefinitions = new RowDefinitionCollection {
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }
};
grid.ColumnDefinitions = new ColumnDefinitionCollection {
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
};
The following doesn't work:
<Grid x:Name="grid" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
...
</Grid>
From the documentation I managed to fetch only c# implementation
I also get the same behaviour (does not fill and expand) for a grid with a single cell (1 row / column, although I am not sure why we would ever need a grid with a single cell full size of screen), but it seems to work fine for a 2 x 2, 3 x 3 cell grid (have not tried others yet).
The Height="" and Width="" attributes are required in Xamarin Forms although I "think" they are not needed in WPF as the default assumes this is the case.
<ContentPage.Content>
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Text="1"></Button>
<Button Grid.Row="1" Grid.Column="1" Text="2"></Button>
</Grid>
</ContentPage.Content>
Here is a sample
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="Top Left" Grid.Row="0" Grid.Column="0" />
<Label Text="Top Right" Grid.Row="0" Grid.Column="1" />
<Label Text="Bottom Left" Grid.Row="1" Grid.Column="0" />
<Label Text="Bottom Right" Grid.Row="1" Grid.Column="1" />
</Grid>

Resources