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>
Related
I have a xamarin.forms app. A screen has many controls, and I use ScrollView to let the user ability to see all the controls. I have a button outside of the ScrollView, because I want it to be easily accessible without scrolling down the screen. But some of our customers have vision issues, so they enlarge the text size on their phones. Then the button disappears from the screen. I wonder what I can do to fix this... I guess I would agree with the scrolling only for those with the large text size, if there is no better solution.
Here is my code:
<ContentPage.Content>
<StackLayout>
<ScrollView>
<StackLayout Margin="30">
... Many controls are here
</StackLayout>
</ScrollView>
<StackLayout
x:Name="Validation"
HeightRequest="150"
IsVisible="{Binding ValidationResult.IsValid, Converter={converters:InverseBoolConverter}}">
<Label Text="Please fix the following errors:" TextColor="Red" Margin="0,0,0,10" />
<Label Text="{Binding Errors}" TextColor="Red" />
</StackLayout>
<Button
Text="Calculate"
Command="{Binding CalculateCommand}"
Style="{StaticResource ButtonStyle}"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
ADDED:
I did not mention that the content of the page is within a ControlTemplate:
<ControlTemplate x:Key="MainPageTemplate">
<Grid>
<Label Text="{Binding ErrorMessage}" TextColor="Red" />
<ContentPresenter Margin="10, 0, 10, 120" />
<StackLayout VerticalOptions="End" BackgroundColor="LightGray" Padding="20" >
<Label Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='© {0:yyyy} Company Name, Inc.'}" />
<Label Text="All trademarks shown are the intellectual properties of their respective owners." />
</StackLayout>
</Grid>
</ControlTemplate>
Agree with #Jason , StackLayout will not fit the size of its child Elements . So you could use Grid with three Rows
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.9*" />
<RowDefinition Height="150" />
<RowDefinition Height="0.1*" />
</Grid.RowDefinitions>
<ScrollView Grid.Row="0" Orientation="Both">
<StackLayout Margin="30" BackgroundColor="LightBlue" HeightRequest="300">
</StackLayout>
</ScrollView>
<Grid HeightRequest = "150" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Text="Please fix the following errors:" TextColor="Red" Margin="0,0,0,10" />
<Label Grid.Row="1" Text="{Binding Errors}" TextColor="Red" />
</Grid>
<Button
Grid.Row="2"
Text="Calculate"
HorizontalOptions="CenterAndExpand" />
</Grid>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Padding="6,2,6,2">
<Frame BackgroundColor="White" CornerRadius="5" Margin="10,5" HasShadow="False" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
</Frame><Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Text="Item1" />
<Label Grid.Row="1" Text="Item2" />
<Label Grid.Row="2" Text="Item3" />
</Grid>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
While with the initial load of the page UI looks like 3rd label is cropped and if I revisits the page the 3rd label showing correctly.how to show the UI perfectly with the initial load itself.issue is only occuring in ios.
From shared code, I have tested in local site on iOS. However, there is no problem in iOS device.
In addition, there is problems about this Xaml code. The Frame layout not contains the Grid , the running effect as follows:
If I put it inside will show as follows:
I also tested in Android device, however it cropped the third lable. If want to make it shows fully, we can add RowHeight for ListView, as follows:
<ListView x:Name="listview" RowHeight="125">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Padding="6,2,6,2" BackgroundColor="AliceBlue">
<Frame BackgroundColor="White"
CornerRadius="5"
Margin="10,5"
HasShadow="False"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Text="Item1" />
<Label Grid.Row="1"
Text="Item2" />
<Label Grid.Row="2"
Text="Item3" />
</Grid>
</Frame>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The effect:
I have a situation where I need to "overlay" a grouped set of information, presented by way of a Card (inplemented as a ContentView) on top of a portion of the page header, which is defined in a Grid inside of a FlexLayout. The output should resemble something like the following where the header is the portion in red:
The code for the header is as follows:
<!-- Header -->
<FlexLayout
HeightRequest="108"
AlignItems="Center"
Direction="Column"
BackgroundColor="#D92732">
<Grid VerticalOptions="Center" Margin="0, 30, 0, 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="40*"></ColumnDefinition>
<ColumnDefinition Width="80*"></ColumnDefinition>
<ColumnDefinition Width="300*"></ColumnDefinition>
<ColumnDefinition Width="80*"></ColumnDefinition>
<ColumnDefinition Width="40*"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ImageButton Source="Bell.png" Grid.Column="1" BackgroundColor="Transparent"
WidthRequest="37"
HeightRequest="40"
Aspect="AspectFit">
</ImageButton>
<Label
Grid.Column="3"
Text="Payments"
TextColor="#F9F8FA"
FontSize="20"
HeightRequest="40"
HorizontalOptions="Center"
VerticalOptions="Center" VerticalTextAlignment="End">
</Label>
<ImageButton Source="Bell.png" Grid.Column="5" BackgroundColor="Transparent"
WidthRequest="37"
HeightRequest="40"
Aspect="AspectFit">
</ImageButton>
</Grid>
</FlexLayout>
The method I used to solve the problem of having a visual element overlay a portion of another visual element, in this case a Grid overlay a portion of another Grid, is to set the position of the overlay Grid to a specific row and column of the first Grid and then set the Bottom value of the overlay Grid's Margin to a negative number that represents the desired height of the overlay Grid, as seen in the following code and screenshot. Note that I enclosed the overlaid Grid in a Frame which is where I set the negative Bottom Margin value.
<Frame Grid.Row="2" Grid.ColumnSpan="3" Grid.Column="0"
Margin="16,30,16,-310" WidthRequest="343" HeightRequest="300">
<StackLayout HeightRequest="350">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="44*" />
<RowDefinition Height="38*" />
<RowDefinition Height="40*" />
<RowDefinition Height="20*" />
<RowDefinition Height="96*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
FontFamily="{StaticResource HNMedium}"
FontSize="{StaticResource Font20}"
HorizontalOptions="Start"
Text="Verify Account"
TextColor="{StaticResource NGIC_DarkGray}"
VerticalOptions="Start" />
<Label Grid.Row="1"
Text="Enter the verification code sent to ******8998"
FontSize="{StaticResource Font16}"
TextColor="{StaticResource NGIC_DarkGrayishBlue}"
FontFamily="{StaticResource HNRegular}"/>
<Entry Grid.Row="2" TextColor="Black" />
<Label Grid.Row="3"
Text="Send a new code"
TextColor="{StaticResource NGIC_Red}"
FontSize="{StaticResource Font14}"
FontFamily="{StaticResource HNMedium}"/>
<StackLayout Grid.Row="4" >
<Button Text="Next"
TextColor="{StaticResource White}"
BackgroundColor="{StaticResource NGIC_Red}"
CornerRadius="15"
WidthRequest="300"
HeightRequest="40"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
FontFamily="{StaticResource HNBold}"
FontSize="{StaticResource Font14}"/>
<Button Text="Cancel"
TextColor="{StaticResource NGIC_Red}"
BackgroundColor="{StaticResource White}"
CornerRadius="15"
WidthRequest="300"
HeightRequest="40"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
FontFamily="{StaticResource HNRegular}"
FontSize="{StaticResource Font14}"
BorderColor="{StaticResource NGIC_Red}"
BorderWidth="1"/>
</StackLayout>
</Grid>
</StackLayout>
</Frame>
I am having trouble with height problems in Xamarin PCL with Listview. I want to create a comments section in a scrollable page. But i am struggling with the height of the comments. It just wont expand.
Row.Definition = Auto for the Grid:
<StackLayout Grid.Row="4" Padding="0,20,0,0">
<ListView ItemsSource="{Binding CommentsMVVM}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<local:CommentsRow />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
In the CommentsRow template:
<Grid RowSpacing="0" ColumnSpacing="14" Padding="20" VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.RowSpan="3" Source="{ Binding Image }" VerticalOptions="Start" />
<Label Grid.Column="1" Grid.Row="0" Text="{ Binding UserName }" VerticalOptions="EndAndExpand" TextColor="Blue" />
<Label Grid.Column="1" Grid.Row="1" Margin="0,4,0,0" Text="{ Binding CreatedAt }" TextColor="Grey" FontSize="13" />
<StackLayout Grid.Column="1" Grid.Row="2" Spacing="30" x:Name="StackLayoutMap" VerticalOptions="FillAndExpand" />
</Grid>
And at the C#:
public CommentsRow()
{
InitializeComponent();
var html_label = new HtmlFormattedLabel()
{
TextColor = Color.Black,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
};
html_label.SetBinding(HtmlFormattedLabel.TextProperty, "Memo");
this.StackLayoutMap.Children.Add(html_label);
}
But the StackLayout comments text just wont expand automatically. What am i doing wrong?
I am facing one issue in xamarin.forms. I am using GridView to make login page design so it can be change accordingly device height and width. I don't want scroll that page So I didn't put any ScrollView.
Xaml Design Code :-
<?xml version="1.0" encoding="utf-8" ?>
<custom:KeyboardResizingAwareContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HRMS.Login.LoginPage"
BackgroundImage="login_bg.png"
xmlns:local="clr-namespace:HRMS;assembly=HRMS"
Title="{x:Static local:AppResources.TitleLogin}"
xmlns:custom="clr-namespace:HRMS.Login;assembly=HRMS">
<ContentPage.Padding>
<OnIdiom x:TypeArguments="Thickness" Phone="60,40,60,40" Tablet="120,100,120,100" />
</ContentPage.Padding>
<custom:KeyboardResizingAwareContentPage.Resources>
<ResourceDictionary>
<Style x:Key="SubTitleStyle" TargetType="Label">
<Setter Property="FontSize" Value="Small" />
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="TextColor" Value="White" />
</Style>
</ResourceDictionary>
</custom:KeyboardResizingAwareContentPage.Resources>
<custom:KeyboardResizingAwareContentPage.Content>
<AbsoluteLayout x:Name="AbsMain">
<Grid VerticalOptions="Center" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
<Grid.RowDefinitions>
<RowDefinition Height="35*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="6*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="6*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image x:Name="ImgLogo" Source="login_logo.png" HorizontalOptions="Center" Grid.Row="0"/>
<StackLayout Grid.Row="1">
<StackLayout.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="55" Phone="35" />
</StackLayout.HeightRequest>
</StackLayout>
<Frame OutlineColor="#DCDCDC"
HorizontalOptions="FillAndExpand" VerticalOptions="Center"
HasShadow="false" Padding="1"
Grid.Row="2"
BackgroundColor="White"
Margin="5,0,5,0">
<Frame.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="55" Phone="35" />
</Frame.HeightRequest>
<Frame.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="9*"/>
</Grid.ColumnDefinitions>
<Image x:Name="ImgLoginName" Grid.Column="0" Source="ico_login_name.png"
HorizontalOptions="StartAndExpand" VerticalOptions="Center" Margin="5,0,0,0"/>
<StackLayout Grid.Column="1" VerticalOptions="Center">
<Entry x:Name="TxtUsername" TextColor="#f15a23"
Margin="5,0,0,0"
PlaceholderColor="#8c8c8c"
BackgroundColor="White"
Placeholder="{x:Static local:AppResources.PHEmployeeCode}" />
</StackLayout>
</Grid>
</Frame.Content>
</Frame>
<Frame OutlineColor="#DCDCDC"
HorizontalOptions="FillAndExpand" VerticalOptions="Center"
HasShadow="false" Padding="1"
Grid.Row="3"
BackgroundColor="White"
Margin="5,0,5,0">
<Frame.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="55" Phone="35" />
</Frame.HeightRequest>
<Frame.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="9*"/>
</Grid.ColumnDefinitions>
<Image x:Name="ImgLoginPassword" Grid.Column="0" Source="ico_login_password.png"
HorizontalOptions="StartAndExpand" VerticalOptions="Center" Margin="5,0,0,0"/>
<StackLayout Grid.Column="1" VerticalOptions="Center">
<Entry x:Name="TxtPassword" IsPassword="True" TextColor="#f15a23"
PlaceholderColor="#8c8c8c"
Margin="5,0,0,0"
BackgroundColor="White"
Placeholder="{x:Static local:AppResources.PHPassword}" />
</StackLayout>
</Grid>
</Frame.Content>
</Frame>
<Label x:Name="LblForgotPassword" Grid.Row="4" Margin="5,0,5,0"
Text="Forgot Password?" TextColor="Black" HorizontalTextAlignment="End"/>
<Button x:Name="BtnLogin" Text="{x:Static local:AppResources.BLogin}" Clicked="LoginButton_Clicked"
Grid.Row="5" BackgroundColor="#f15a23" TextColor="White"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Button.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="55" Phone="45" />
</Button.HeightRequest>
</Button>
<Image x:Name="ImgOr" Grid.Row="6" HorizontalOptions="FillAndExpand"
Source="or.png" />
<Button x:Name="BtnUpdateDevicewithlogin" Text="{x:Static local:AppResources.BUpdateDevice}"
Clicked="UpdateDeviceWithLoginButton_Clicked" Grid.Row="7"
BackgroundColor="#f15a23" TextColor="White" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Button.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="55" Phone="45" />
</Button.HeightRequest>
</Button>
</Grid>
<ActivityIndicator x:Name="actIndicator2" Color="#f15a23" Opacity="2"
HeightRequest="50" WidthRequest="50" VerticalOptions="Center" HorizontalOptions="Center"
AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" />
</AbsoluteLayout>
</custom:KeyboardResizingAwareContentPage.Content>
</custom:KeyboardResizingAwareContentPage>
I am using "KeyboardResizingAwareContentPage" page instead "ContentPage" because In ios Page is not set when keyboard is open. password field hide behind keyboard so users can not see whatever then typing. That's why I am using Render for content Page in IOs.
As Above images when I focus on username or password entry field two buttons are hide behind the keyboard. Client requirement is that at that time page should be scroll. How can I solve this problem? Is it possible through rendering? I am facing this issue in both the platform. (ios and android)
Even if you don't mean to scroll your page, use a ScrollView as the root of your Page, and put your grid in it.
This should fix it.