I'm making an Entry that has an Image on the right inside it.
The problem I have is that the text is placed behind the Image, what I want to do is that the text is cut off when it reaches the Image, separate the entry from the Image. I am using a Frame and inside it, a Grid. I have tried to put right margin to Entry but the Image is scrolling out. To do the Entry I am using the NuGet: Xamarin.Forms.Visual.Material
This is my code:
<StackLayout>
<Frame BackgroundColor="Transparent" HasShadow="False">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Entry
x:Name="entrycontrol"
BackgroundColor="{Binding Source={x:Reference ValidateView}, Path=BGColor}"
HeightRequest="60"
Visual="Material"
WidthRequest="{Binding Source={x:Reference ValidateView}, Path=Width}" />
<Image
x:Name="ImageRight"
Grid.Column="0"
Margin="0,0,5,0"
HeightRequest="25"
HorizontalOptions="End" />
</Grid>
</Frame>
</StackLayout>
The Source of the Image I establish it in the code behind.
Your main issue is that you are overlaying your Image on top of the Entry control. You need to specify a Grid.Column="1" so it uses the right hand column. A detailed explanation of how a Grid layout works can be found on Microsofts site
I would suggest that you do not need to bind the WidthRequest property for the Entry control. Plus I would swap your column definitions around. Auto means the column will grow in size, * means it will stretch to a size based on what other columns have been defined.
To summarise you want something like:
<StackLayout>
<Frame BackgroundColor="Transparent" HasShadow="False">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" /> <!-- swapped -->
<ColumnDefinition Width="Auto" /> <!-- swapped -->
</Grid.ColumnDefinitions>
<Entry x:Name="entrycontrol"
BackgroundColor="{Binding Source={x:Reference ValidateView}, Path=BGColor}"
HeightRequest="60"
Visual="Material" />
<Image x:Name="ImageRight"
Grid.Column="1"
Margin="0,0,5,0"
HeightRequest="25"
HorizontalOptions="End" />
</Grid>
</Frame>
</StackLayout>
Better yet if you have a fixed image size then you can simply set your ColumnDefinition to that size.
Related
i have this sample xaml:
<StackLayout Orientation="Horizontal"
HorizontalOptions="CenterAndExpand">
<StackLayout BackgroundColor="LightBlue">
<Label Text="ABC"
HorizontalTextAlignment="Center" />
<Label Text="L"
HorizontalTextAlignment="Center"></Label>
</StackLayout>
<BoxView BackgroundColor="Red" />
<StackLayout BackgroundColor="LightGreen">
<Label Text="ABC"
HorizontalTextAlignment="Center" />
<Label Text="C"
HorizontalTextAlignment="Center"></Label>
</StackLayout>
</StackLayout>
which produces this output:
But if one of the four lables becomes bigger there are not centered anymore:
My goal is the following layout, which depends on the biggest label:
I'm a beginner. So i hope somebody could help me with the correct layout.
Thanks
StackLayout really isn't the best choice here, because it occupies the size dynamically (i.e. depending on its content). What's the better approach here is to use Xamarin.Forms' Grid layout.
This way, you can have a grid with 3 columns. Then, you will set your ColumnDefinitions's width to star (*). Like you can see in the Rows and columns section, setting the column's width to star means that:
Star – leftover row height or column width is allocated proportionally (a number followed by * in XAML).
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackLayout Grid.Column="0" /> <!-- Blue StackLayout wrapper -->
<BoxView Grid.Column="1" /> <!-- Red BoxView -->
<StackLayout Grid.Column="2" /> <!-- Green StackLayout wrapper -->
</Grid>
Basically, you will have divided the whole screen width to 3 equal parts, each resulting in an 33,3(3) % of the screen. Then, you can align the elements in the container as you wish.
I am new to Xamarin forms and Xaml, trying to design a page layout with a background image, I found out that using Relative layout, I can get the image expand to whole screen using aspect property. However, When I put a grid or layout after the image inside RelativeLayout, It does not expand to whole area, It only covers the given content.
I have tried to achieve this using relative layout. I am using an Image tag with aspect property and then my stacklayout on top of image.
<RelativeLayout>
<Image Aspect="AspectFill" Source="c1.jpg" />
<StackLayout>
<Button Text="Meha" TextColor="White"/>
</StackLayout>
</RelativeLayout>
I want the button to cover whole width and align it vertically center.
This is how i normally solve this situation:
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="White">
<Image Source="c1.jpg" Aspect="AspectFill" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center">
<Button Text="Meha" TextColor="Black" HorizontalOptions="FillAndExpand"/>
</StackLayout>
</Grid>
Another way you can try is to use a grid strategy instead of a relative layout. like this example of a welcome page that i use in my app with a bg image:
<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<!---BG IMAGE -->
<Image
Source="yourimage"
Aspect="AspectFill"
Opacity="0.5"
/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto"/>
<RowDefinition
Height="Auto"/>
<RowDefinition
Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="*" />
<ColumnDefinition
Width="100" />
<ColumnDefinition
Width="*" />
</Grid.ColumnDefinitions>
<!--TEXT-->
<StackLayout
Grid.Row="1"
Grid.ColumnSpan="3"
Spacing="10"
Orientation="Vertical"
VerticalOptions="Center"
TranslationY="-20"
Padding="20">
<Label
LineBreakMode="WordWrap"
Text="Welcome!"
TextColor="White"
VerticalTextAlignment="Center"
FontAttributes="Bold"
>
<Label.FontSize>
<OnIdiom
x:TypeArguments="x:Double"
Phone="26"
Tablet="36" />
</Label.FontSize>
</Label>
<Setter
Property="HeightRequest"
Value="4" />
<Setter
Property="VerticalOptions"
Value="End" />
<Setter
Property="WidthRequest"
Value="40" />
<Setter
Property="BackgroundColor"
Value="{ DynamicResource AccentColor }"
<!-- ACCENT LINE -->
<BoxView VerticalOptions= "End"
HeightRequest = "4"
BackgroundColor="Green" />
</StackLayout>
</Grid>
</Grid>
I have a Label in my AbsoluteLayout and when that Label's text is long in puts text into two lines which is okay but is it possible to get a Margin to the second left wrapped line too? I attached image to show better what I mean and in that Image there is Red arrow showing the place what I'm asking.
Thanks for All!
<Grid HorizontalOptions="Center"
VerticalOptions="Center">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<AbsoluteLayout HorizontalOptions="Start">
<Image HeightRequest="160"
HorizontalOptions="Start"
WidthRequest="360"
Aspect="AspectFill"
Grid.Row="0"
Source="{Binding EnclosureUrl}"
Margin="0,0,0,15">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TappedItemCommand"
CommandParameter="{Binding Link}"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="0" Grid.Column="0"
HorizontalOptions="Start"
Margin="0,0,0,40"
WidthRequest="250"
Style="{StaticResource BoldBasicText}"
Font="Bold,20"
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds="0,1,-1,-1"
BackgroundColor="{x:Static ownStyles:StyleSettings.colCaptionColorFade}"
TextColor="{x:Static StyleSettings:Settings.colRed}"
Text="{Binding Header}">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TappedItemCommand"
CommandParameter="{Binding Link}"
NumberOfTapsRequired="1" />
</Label.GestureRecognizers>
</Label>
</AbsoluteLayout>
</Grid>
You can use Line Break Mode available in Xamarin.Forms:-
LineBreakMode="WordWrap"
<Label Text="{Binding Certification, StringFormat='Board Certification:{0:F0}'}" LineBreakMode="WordWrap"/>
Refer Link Below:-
Xamarin Text Line Break Mode
Label wrapping with StackLayout
My XAML looks like this:
<StackLayout Orientation="Vertical" HorizontalOptions="Center">
<controls:BindablePicker HorizontalOptions="Center" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListView Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid BackgroundColor="Aqua">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<controls:BindablePicker Title="Length" Grid.Row="0" Grid.Column="0" BackgroundColor="White" />
<controls:BindablePicker Title="Units" Grid.Row="0" Grid.Column="1" BackgroundColor="White" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</StackLayout>
Here's what I'm getting: Attempt
This is pretty much what I want, except I want the content centered within the aqua area so everything is centered horizontally. I don't mind if the left cell is right justified and the right cell is left justified.
Note that I'm wanting to add other content beneath this, like labels and other grids, all centered on the page.
Any help would be greatly appreciated. Thanks.
First of all I do not think that you need a row definition in your inner grid since you have only one row.
Secondly for performance sake,do not user "Auto" , just remove the width property from the column definitions.
By doing this both columns will have the same width... I hope ;)
I think if you made your list view like below then it would fix it, but also try it without HorizontalOptions for the controls.
<ListView Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid BackgroundColor="Aqua">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<controls:BindablePicker Title="Length" Grid.Column="0" BackgroundColor="White" HorizontalOptions = "End" />
<controls:BindablePicker Title="Units" Grid.Column="1" BackgroundColor="White" HorizontalOptions = "Start" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Hint: The next release of Xamarin.Forms, which is in the alpha channel now, contains a bindable picker so you do not have to user a 3rd party now.
I have 3 controls in a Grid.Row but how can I make them use the full width of the page?
This is my xaml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Grid.Row="1"
Source="/Assets/image1.png"
Height="25"
Width="25" />
<TextBox Grid.Column="1"
Margin="10,0,0,0"
Text="{Binding InputText, Mode=TwoWay}"
BorderBrush="Black"
BorderThickness="2"
VerticalAlignment="Center">
</TextBox>
<Button Grid.Column="2"
BorderThickness="2"
HorizontalAlignment="Right"
Command="{Binding AddTextCommand}"
Margin="10,0,0,0">
<TextBlock x:Uid="ButtonText" />
</Button>
</Grid>
This is now the result:
As you can see it is aligned left, how can I make it use the full width?
You're code only shows 3 controls (Image, TextBox, Button), while your screenshot gives 4 controls. I suppose the full width control on top is missing, but that's no problem to answer the question.
If we break down your XAML, you have:
First column width Auto, filled with an image.
Second column width 140, filled with a TextBox
Third column width * (or the rest of the space), filled with a Button
On the button you have placed HorizontalAlignment="Right" (default is Left), in which you're saying: only use the space necessary and put the control on the right side. If you want to use the full width available, you have to use HorizontalAlignment="Stretch".
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="140" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Source="/Assets/image1.png"
Height="25"
Width="25" />
<TextBox Grid.Column="1"
Margin="10,0,0,0"
Text="{Binding InputText, Mode=TwoWay}"
BorderBrush="Black"
BorderThickness="2"
VerticalAlignment="Center">
</TextBox>
<Button Grid.Column="2" x:Uid="ButtonText"
BorderThickness="2"
HorizontalAlignment="Stretch"
Command="{Binding AddTextCommand}"
Margin="10,0,0,0" />
</Grid>
Note that I have also removed the TextBlock from the Button and move the x:Uid tag to the button. Simply use ButtonText.Content in your resources to have your button localized, there's no need to place a TextBlock in there.