Grid does not fit the screen on Xiaomi - xamarin.forms

I have a somewhat unusual problem and I did not find any references in the searches I performed.
I have a Xamarin Forms application and on the login screen the Grid does not adjust to the layout and does not display any controls on the screen.
To make it easier to understand, I made the screen as simple as possible by replacing the background image with a solid color one.
I've already changed the login screen several times, but it didn't work. I've tried with the Grid, without the Grid, Grid with lines, Grid without lines, a Stacklayout for each line of the Grid, a single Stacklayout, with Frame, without Frame, but nothing solves.
And the worst thing is that in the tests done by the robot on firebase, they found no errors.
This only happens on some Xiaomi (on other devices, even Xiaomi, it doesn't).
I'm using VS 2019 and Xamarin Forms 4.8.0.1269.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:MySample.Controls"
BackgroundImage="solid.png"
x:Class="MySample.Views.LoginView">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,20,0,0" />
</OnPlatform>
</ContentPage.Padding>
<ContentPage.Content>
<Grid x:Name="gridExterno"
RowSpacing="0"
ColumnSpacing="0"
Padding="30,20,30,5"
BackgroundColor="White"
Margin="30,5"
VerticalOptions="CenterAndExpand" HorizontalOptions="Center">
<Grid.MinimumHeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="600" Phone="300" Desktop="300"/>
</Grid.MinimumHeightRequest>
<Grid.MinimumWidthRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="800" Phone="600" Desktop="600"/>
</Grid.MinimumWidthRequest>
<Grid.WidthRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="800" Phone="600" Desktop="600"/>
</Grid.WidthRequest>
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="logo.png" VerticalOptions="Center" HorizontalOptions="Center" Margin="0,10,0,10">
<Image.WidthRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="350" Phone="300" Desktop="300"/>
</Image.WidthRequest>
</Image>
<StackLayout
Orientation="Vertical"
Grid.Row="1"
VerticalOptions="Center"
HorizontalOptions="Center"
Spacing="15"
Margin="10">
<Entry x:Name="EntryLogin"
TextChanged="OnEntryLoginTextChanged"
MaxLength="20"
Text=""
Keyboard="Numeric"
HorizontalOptions="FillAndExpand">
<Entry.WidthRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="400" Phone="300" Desktop="300"/>
</Entry.WidthRequest>
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="75" Phone="50" Desktop="50"/>
</Entry.HeightRequest>
<Entry.Margin>
<OnIdiom x:TypeArguments="Thickness" Tablet="0,0,0,20" Phone="0" Desktop="0"/>
</Entry.Margin>
<Entry.FontSize>
<OnIdiom x:TypeArguments="x:Double" Tablet="30" Phone="20" Desktop="20" />
</Entry.FontSize>
</Entry>
<Entry x:Name="EntryPassword"
Text=""
MaxLength="20"
IsPassword="True"
HorizontalOptions="FillAndExpand">
<Entry.Margin>
<OnIdiom x:TypeArguments="Thickness" Tablet="0,0,0,20" Phone="0" Desktop="0"/>
</Entry.Margin>
<Entry.WidthRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="400" Phone="300" Desktop="300"/>
</Entry.WidthRequest>
<Entry.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="75" Phone="50" Desktop="50"/>
</Entry.HeightRequest>
<Entry.FontSize>
<OnIdiom x:TypeArguments="x:Double" Tablet="28" Phone="20" Desktop="20"/>
</Entry.FontSize>
</Entry>
<StackLayout Orientation="Horizontal" Spacing="16" HorizontalOptions="FillAndExpand">
<StackLayout.Margin>
<OnIdiom x:TypeArguments="Thickness" Tablet="2,0" Phone="0" Desktop="0"/>
</StackLayout.Margin>
<CheckBox x:Name="ckRemember"/>
<Label Text="Remember Me" VerticalOptions="Center">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double" Tablet="26" Phone="18" Desktop="18"/>
</Label.FontSize>
</Label>
</StackLayout>
<Button x:Name="btnEntrar1"
Text="Enter"
BackgroundColor="{StaticResource ButtonPrimary}"
Clicked="OnLoginAsync"
TextColor="White"
HorizontalOptions="FillAndExpand">
<Button.WidthRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="400" Phone="300" Desktop="300"/>
</Button.WidthRequest>
<Button.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Tablet="75" Phone="50" Desktop="50"/>
</Button.HeightRequest>
<Button.FontSize>
<OnIdiom x:TypeArguments="x:Double" Tablet="18" />
</Button.FontSize>
<Button.Margin>
<OnIdiom x:TypeArguments="Thickness" Tablet="0,15,0,0" Phone="0" Desktop="0"/>
</Button.Margin>
</Button>
<Label x:Name="lblVersion"
VerticalOptions="End"
HorizontalTextAlignment="Center"
HorizontalOptions="Center">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double" Tablet="20" Phone="16" Desktop="16"/>
</Label.FontSize>
<Label.Margin>
<OnIdiom x:TypeArguments="Thickness" Tablet="0,0,0,10" Phone="0,3,0,5" Desktop="0,3,0,5"/>
</Label.Margin>
</Label>
</StackLayout>
</Grid>
</ContentPage.Content>
</ContentPage>

Change the x:Name="gridExterno" HorizontalOptions to HorizontalOptions="CenterAndExpand"

Related

How to prevent Scrollview expand beyond its maximum usable space for expanding

I think i have tried everything possible but i'm also sure that i have the nose too close from the window to see clearly after 5 days trying constraining the expansion of the scrollviewer. I would like it to remain under the stact layout of the thumbnail viewer. Represented by the red line. Any help or solution would be greatly appreciated.
<?xml version="1.0" encoding="UTF-8" ?>
<Frame
x:Class="EchoMessager.Pages.ListeMessages.MessageEditor"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:resx="clr-namespace:EchoMessager.Resx"
xmlns:k="clr-namespace:EchoMessager;assembly=EchoMessager"
xmlns:local="clr-namespace:EchoMessager"
xmlns:ctrls="clr-namespace:EchoMessager.Controls;assembly=EchoMessager"
xmlns:ctrl="clr-namespace:EchoMessager.Controls.MessageEditor;assembly=EchoMessager"
x:Name="EditorControlsPresenterContainer"
VerticalOptions="End">
<FlexLayout
Direction="Column"
JustifyContent="End">
<!-- Thumbnail Presenter -->
<StackLayout
x:Name="ThumbnailPlaceHolder"
Margin="0"
Padding="0"
Orientation="Vertical"
VerticalOptions="End"
Spacing="0"
BackgroundColor="#545454">
<ctrls:CloseOpenSwitch
x:Name="PreviewPresenterControlSwith"
IsVisible="True"
VerticalOptions="End"
State="disabled" />
<!-- Row #2 - Thumbnails Viewer Implementation -->
<CollectionView
x:Name="ThumbnailsPresenter"
Margin="0"
VerticalOptions="End"
SelectionMode="Single"
IsVisible="true"
HeightRequest="95"
MinimumHeightRequest="95"
ItemSizingStrategy="MeasureAllItems"
HorizontalScrollBarVisibility="Never"
VerticalScrollBarVisibility="Never">
<CollectionView.ItemsLayout>
<LinearItemsLayout
Orientation="Horizontal"
SnapPointsType="MandatorySingle"
SnapPointsAlignment="Center"
ItemSpacing="8" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="{x:Type ctrl:ThumbnailModel}">
<Image
Margin="1"
Aspect="AspectFit"
Source="{Binding Thumbnail, Mode=OneWay}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OpenCommand, Mode=OneWay}" />
</Image.GestureRecognizers>
</Image>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
<!-- Editor Row -->
<StackLayout
x:Name="EditorControlsPresenter"
Margin="0"
Padding="0"
BackgroundColor="GreenYellow"
Orientation="Horizontal"
VerticalOptions="End">
<Image
x:Name="cameraButton"
Margin="0"
VerticalOptions="Center"
FlexLayout.AlignSelf="Start"
WidthRequest="60"
HeightRequest="60"
BackgroundColor="Transparent"
Source="camerabutton.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnPrendrePhoto" />
</Image.GestureRecognizers>
</Image>
<ScrollView
x:Name="MessagesViewScrollingArea"
Margin="0"
Padding="0"
HorizontalOptions="StartAndExpand"
VerticalOptions="End"
VerticalScrollBarVisibility="Never"
HorizontalScrollBarVisibility="Never">
<!-- What is about to be scroll -->
<Editor
x:Name="EntrerMessage"
Margin="0"
VerticalOptions="End"
FlexLayout.Grow="1"
AutoSize="TextChanges"
Keyboard="Default"
MaxLength="1500"
Placeholder="{x:Static resx:AppResources.MessageEntryPlaceholder}"
Text="{Binding Message.Texte, Mode=TwoWay}"
Unfocused="EntrerMessage_OnUnfocused"
FontSize="{OnPlatform Default=Default,
Converter={StaticResource FontNamedSizeToDouble}}">
</Editor>
</ScrollView>
<AbsoluteLayout
HorizontalOptions="End"
VerticalOptions="Center">
<Image
x:Name="envoyerMessageButton"
Margin="0"
HeightRequest="60"
WidthRequest="60"
Source="sendbutton.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSendMessage" />
</Image.GestureRecognizers>
</Image>
<ActivityIndicator
x:Name="activityIndicator"
Margin="0"
HeightRequest="60"
WidthRequest="60"
IsRunning="false"
IsVisible="false"
Opacity="0.8"
Color="Black" />
</AbsoluteLayout>
</StackLayout>
<!-- Editor tools -->
<!-- Bouton Coller -->
<Button
x:Name="boutonCollerMessage"
Margin="0"
Padding="10,0"
IsVisible="false"
FontSize="Micro"
BackgroundColor="Blue"
FlexLayout.AlignSelf="Stretch"
VerticalOptions="CenterAndExpand"
Text="Coller le message"
Clicked="OnCollerMessage" />
</FlexLayout>
</Frame>
Ok, there is a seriously strange bug with xamarin.forms and we found around here that fwe must put this proporty to true on a stacklayout containing many children to make allow it to behave as expected. simply add stacklayoutname.IsTabStop=False;

Top positioned Xamarin forms listview with a header - problem: top position varies on some iPhone models

I need to set the height of the list view so that the bottom view cell is not cut off whenever adding or removing a view item. I can achieve it for some iPhone models, but not the others. It seems it’s not a right way to achieve it by iPhone model. My questions are:
1. On some iPhone models, why the top position shifts down by about 25 pixels after adding a view item?
2. How to find the absolute top position Y coordinate of the list view?
It seems AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" doesn’t help. VerticalOptions="Start" doesn’t help either.
Note:
1. no such an issue on Android phones.
2. Observation only: It seems that changing the margin of the listview header brandStack has no effect on the layout, eg. cannot see increased margins.
3. The following two screenshots show that the top position of the listview header has moved down a bit after an item is added to the listview.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="tecommobile.Views.PanelsHomePage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:effects="clr-namespace:tecommobile.Effects;assembly=tecommobile"
xmlns:tecommobile="clr-namespace:tecommobile;assembly=tecommobile"
xmlns:views="clr-namespace:tecommobile.Views;assembly=tecommobile"
xmlns:controls="clr-namespace:Flex.Controls;assembly=Flex"
xmlns:system="clr-namespace:System;assembly=netstandard"
x:Name="PanelHomePage"
Title="{Binding Title}"
BackgroundColor="Bisque"
NavigationPage.HasNavigationBar="false">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="addPanelStyle" TargetType="views:CustomImageButton" BasedOn="{StaticResource customImageButtonStyle}">
<Setter Property="Source" Value="panel_add.png" />
<Setter Property="HeightRequest" Value="70" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<RelativeLayout>
<tecommobile:GradientColorStack RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=1}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=1}"
Style="{StaticResource gradientColorStack}">
<StackLayout>
<StackLayout x:Name="panelListViewStack" BackgroundColor="Red"
AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
<ListView x:Name="ItemsListView" BackgroundColor="Orange"
Margin="15"
CachingStrategy="RecycleElement"
IsPullToRefreshEnabled="false"
ItemsSource="{Binding Items}"
RefreshCommand="{Binding LoadItemsCommand}"
SelectionMode="None"
Style="{StaticResource listViewNoSeparatorStyle}"
Scrolled="ItemsListView_OnScrolled">
<ListView.Header>
<StackLayout x:Name="brandStack" Margin="15, 0, 15, 0" HeightRequest="150" BackgroundColor="Yellow">
<Image HeightRequest="48"
HorizontalOptions="Center"
Source="Icon_ILXLinks.png"
WidthRequest="48"
Margin="0,30,0,0" />
<Label FontSize="20"
HorizontalTextAlignment="Center"
Text="TecomPlus"
TextColor="White"
VerticalTextAlignment="Center" />
<Label FontSize="14"
HorizontalTextAlignment="Center"
Text="Your smart security manager"
TextColor="White"
VerticalTextAlignment="Center"
Margin="0,0,0,20" />
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid effects:RoundCornersEffect.CornerRadius="8" HeightRequest="75" Margin="2" BackgroundColor="{Binding PanelColour}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{Binding BindingContext.EditToggleIcon, Source={x:Reference PanelHomePage}}"
BackgroundColor="Transparent" WidthRequest="50" HorizontalOptions="StartAndExpand" Margin="10,0,0,0">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.HomePageEditPanelCommand, Source={x:Reference PanelHomePage}}" CommandParameter="{Binding .}"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Text}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" VerticalOptions="FillAndExpand"
LineBreakMode="NoWrap" FontSize="14" TextColor="{Binding PanelTextColour}">
</Label>
<Image Grid.Row="0" Grid.Column="2" Source="forward.png" Style="{StaticResource rightImageStyle}">
</Image>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.RunPanelCommand, Source={x:Reference PanelHomePage}}" CommandParameter="{Binding .}"
NumberOfTapsRequired="1" />
</Grid.GestureRecognizers>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<StackLayout HeightRequest="70"></StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BackToEditCommand}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<tecommobile:GradientColorStack.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BackToEditCommand}"/>
</tecommobile:GradientColorStack.GestureRecognizers>
</tecommobile:GradientColorStack>
<views:CustomImageButton Command="{Binding InfoCommand}" BackgroundColor="Transparent"
HeightRequest="30"
WidthRequest="30"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=1,
Constant=-45}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0,
Constant=35}"
Source="info.png">
</views:CustomImageButton>
<Grid x:Name="bottomGrid" BackgroundColor="#216593" effects:RoundCornersEffect.CornerRadius="8"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-140}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-70}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.0*" />
<ColumnDefinition Width="1.0*" />
</Grid.ColumnDefinitions>
<Image x:Name="AddButton" Source="panel_add.png" Grid.Row="0" Grid.Column="0" Style="{StaticResource buttonImageStyle}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding AddItemCommand}" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Image x:Name="DeleteButton" Source="{Binding BindingContext.DeleteToggleIcon, Source={x:Reference PanelHomePage}}"
Grid.Row="0" Grid.Column="1" Style="{StaticResource buttonImageStyle}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ToggleDeleteIconCommand}" CommandParameter="{x:Reference DeleteButton}" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
</Grid>
</RelativeLayout>
</ContentPage.Content>
</ContentPage>
The top position shifts down because the margin you set in the listView:
<ListView BackgroundColor="Orange" Margin="15">
Remove the Margin="15" and the space will disappear.
BTW, you can add page-safe-area-layout to make it looks better:
<ContentPage ...
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
Title="Safe Area"
ios:Page.UseSafeArea="true">
<StackLayout>
...
</StackLayout>
</ContentPage>

xamarin forms master detail page flow direction in arabic

I have a Master-Detail Page in my project, I've set the phone language to Arabic and everything is working fine the master page is on the right and the icon is on the right. But the thing is the TitleView (toolbaritems) are pushed a little bit to the right when the phone is set to Arabic. I tried this without Master-Detail page project and the TitleView is working fine and it is in the exact place.
With Master Detail Page:
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AppXamarin.Pages"
x:Class="AppXamarin.Pages.MasterMainPage" NavigationPage.HasNavigationBar="False" NavigationPage.HasBackButton="False" FlowDirection="{x:Static Device.FlowDirection}">
<MasterDetailPage.Master>
<local:MasterPage x:Name ="masterPage" FlowDirection="{x:Static Device.FlowDirection}"/>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage FlowDirection="{x:Static Device.FlowDirection}">
<x:Arguments>
<local:MenuPage></local:MenuPage>
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
MenuPage Xaml file:
<NavigationPage.TitleView>
<Grid Grid.Row="0" Grid.Column="0" HorizontalOptions="EndAndExpand" FlowDirection="{x:Static Device.FlowDirection}">
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="cart_Clicked"/>
</Grid.GestureRecognizers>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image x:Name="BadgeIconImage" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="6" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Source="cart">
<Image.HeightRequest>
<OnIdiom Phone="25" Tablet="35"/>
</Image.HeightRequest>
<Image.WidthRequest>
<OnIdiom Phone="25" Tablet="35"/>
</Image.WidthRequest>
</Image>
<Frame x:Name="BadgeFrame" Grid.Row="0" Grid.Column="1" CornerRadius="12" Padding="10,4" HasShadow="false" OutlineColor="Transparent" BackgroundColor="Transparent">
<Label x:Name="BadgeTextLabel" FontSize="12" FontAttributes="Bold" HorizontalOptions="EndAndExpand" TextColor="#FF0000"/>
</Frame>
</Grid>
</NavigationPage.TitleView>
Result:
Without Master Detail Page:
<NavigationPage.TitleView>
<Grid Grid.Row="0" Grid.Column="0" HorizontalOptions="EndAndExpand" FlowDirection="{x:Static Device.FlowDirection}">
<!--<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="cart_Clicked"/>
</Grid.GestureRecognizers>-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image x:Name="BadgeIconImage" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="6" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Source="cart">
<Image.HeightRequest>
<OnIdiom Phone="25" Tablet="35"/>
</Image.HeightRequest>
<Image.WidthRequest>
<OnIdiom Phone="25" Tablet="35"/>
</Image.WidthRequest>
</Image>
<Frame x:Name="BadgeFrame" Grid.Row="0" Grid.Column="1" CornerRadius="12" Padding="10,4" HasShadow="false" OutlineColor="Transparent" BackgroundColor="Transparent">
<Label x:Name="BadgeTextLabel" FontSize="12" FontAttributes="Bold" HorizontalOptions="EndAndExpand" TextColor="#FF0000"/>
</Frame>
</Grid>
<!--</Grid>-->
</NavigationPage.TitleView>
Result:
My understanding tells me you are trying to add toolbar items so you do not use titleView for that:
What you should be doing, on the other hand, is something like below
C#:
this.ToolbarItems.Add(new ToolbarItem()
{
Icon = "BadgeIconImage",
Command = YourCommand,
});
XAML:
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Icon="BadgeIconImage" />
</ContentPage.ToolbarItems>
To understand Xamarin Forms Toolbar check this guide by Adam Pedley
Update
I have a feeling that the padding that you are using on the Frame is what's pushing your View
Padding="10,4"
Check this:
<Frame x:Name="BadgeFrame" Grid.Row="0" Grid.Column="1" CornerRadius="12" Padding="10,4" HasShadow="false" OutlineColor="Transparent" BackgroundColor="Transparent">
Also could be the corner radius.
Yes I did it
manifest :
<application android:label="Sanaap" android:icon="#mipmap/launcher_foreground" android:theme="#style/MainTheme" android:supportsRtl="true">
MainView.xaml
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sanaap.App.Views.MenuView"
FlowDirection="RightToLeft"
Title="Main">

Xamarin Forms Relative Layout trailing spaces

<ContentPage.Content>
<StackLayout VerticalOptions="Start" HorizontalOptions="Fill">
<RelativeLayout HeightRequest="100" BackgroundColor="Blue">
<Image x:Name="dishImageView" Aspect="AspectFit" BackgroundColor="Maroon" RelativeLayout.YConstraint="10" RelativeLayout.XConstraint="10" RelativeLayout.WidthConstraint="80" RelativeLayout.HeightConstraint="80" Source="pizza1.png" />
<Label Text="Dominoz Pizza"
x:Name="pizzaTitle"
RelativeLayout.YConstraint="10"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,
ElementName= dishImageView,
Constant=20,
Property=Width,
Factor=1}"/>
<Label BackgroundColor="Lime" HeightRequest="60" Margin="0,0,20,0" MaxLines="2" LineBreakMode="WordWrap" Text="Dominoz Pixxa is great pizza.. come and eat pizza"
x:Name="pizzaDescription"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView,
ElementName=pizzaTitle,
Constant=10,
Property= Height, Factor=1}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,
ElementName= dishImageView,
Constant=20,
Property=Width,
Factor=1}"
/>
</RelativeLayout>
</StackLayout>
</ContentPage.Content>
Basically I am from iOS background. There we use to have leading and trailing spaces. I am not able to see any trailing space option in Xamarin forms.... Can You Please help me how to set this... Here is the sample image what I am trying to implement.
And here is What I am able to get with above source code.
There are a few blogs around advising to not use RelativeLayout, and use AbsoluteLayout and/or StackLayout instead, for performance reasons. I personally am not a fan of RelativeLayout and will try to use alternatives wherever possible. RelativeLayout is also not fun to play with, and the code can get messy quickly.
The layout you are trying to achieve can indeed be done using RelativeLayout, but I think it'll be much easier to use a series of StackLayouts. For example:
<StackLayout
HeightRequest="100"
BackgroundColor="Blue"
Orientation="Horizontal">
<!-- Image -->
<Image
x:Name="dishImageView"
Aspect="AspectFit"
BackgroundColor="Maroon"
Source="pizza1.png" />
<!-- Image/Item Description -->
<StackLayout
HorizontalOptions="FillAndExpand"
Orientation="Vertical">
<Label
Text="Dominoz Pizza"
x:Name="pizzaTitle"/>
<Label
BackgroundColor="Lime"
HeightRequest="60"
Margin="0,0,20,0"
MaxLines="2"
LineBreakMode="WordWrap"
Text="Dominoz Pixxa is great pizza.. come and eat pizza"
x:Name="pizzaDescription"/>
<!-- Add the price label here -->
</StackLayout>
</StackLayout>
And Here I achieved it Finally using Grid Concept.
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="UdemyFirst.PizzaPage">
<StackLayout>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Image
BackgroundColor="Blue"
Grid.Row="0"
Margin="10"
Grid.Column="0"/>
<StackLayout
BackgroundColor="Fuchsia"
Grid.Row="0"
Grid.Column="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25*"/>
<RowDefinition Height="50*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Label
Text="Pizza"
BackgroundColor="Black"
Grid.Row="0"
Grid.Column="0"
TextColor="White"/>
<Label
Text="Pizza is very bad. You should not eat Pizza. Pizza is very bad."
BackgroundColor="Red"
LineBreakMode="WordWrap"
MaxLines="2"
Grid.Row="1"
Grid.Column="0"
TextColor="White"/>
<Label
Text="10$"
BackgroundColor="Green"
XAlign="End"
Grid.Row="2"
Grid.Column="0"
TextColor="White"/>
</Grid>
</StackLayout>
</Grid>
</StackLayout>
</ContentPage>

AbsoluteLayout doesnt align controls as expected when positions are same

I am trying to get the similar popup as below on Androd, see the X close button is nicely lays on top of the frame. I copied code from the github to test but actual code is also not working. half of the button is always behind the frame. I thought that first added control goes below and 2nd one comes above if their position lays on each other. is there any exception here? whatever I tried i couldnt set button obove the frame. Can somebody sheds some light on usage of absolutelayout here?
<ScrollView
HorizontalOptions="Center"
VerticalOptions="Center">
<AbsoluteLayout>
<Frame
x:Name="FrameContainer"
Margin="15"
HorizontalOptions="Center"
BackgroundColor="White">
<StackLayout
IsClippedToBounds="True"
Padding="10, 5"
Spacing="3">
<Image
HorizontalOptions="Center"
x:Name="OctocatImage"
Margin="10"
HeightRequest="150"
WidthRequest="150">
<Image.Source>
<OnPlatform
x:TypeArguments="ImageSource"
Android="github_octocat.png"
iOS="github_octocat.png"
WinPhone="Assets/github_octocat.png"/>
</Image.Source>
</Image>
<Entry
HorizontalOptions="Center"
x:Name="UsernameEntry"
Style="{StaticResource EntryStyle}"
Placeholder="Username" />
<Entry
HorizontalOptions="Center"
x:Name="PasswordEntry"
Style="{StaticResource EntryStyle}"
Placeholder="Password"
IsPassword="True"/>
<Button
Margin="10, 5"
BackgroundColor="#7dbbe6"
HorizontalOptions="Fill"
Clicked="OnLogin"
x:Name="LoginButton"
Text="Login">
<Button.HeightRequest>
<OnPlatform x:TypeArguments="x:Double" Android="50" iOS="30" WinPhone="30"/>
</Button.HeightRequest>
</Button>
</StackLayout>
</Frame>
<ContentView
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds="1, 0, -1, -1">
<ContentView.GestureRecognizers>
<TapGestureRecognizer Tapped="OnCloseButtonTapped"/>
</ContentView.GestureRecognizers>
<Image
x:Name="CloseImage"
HeightRequest="30"
WidthRequest="30">
<Image.Source>
<OnPlatform
x:TypeArguments="ImageSource"
Android="close_circle_button.png"
iOS="close_circle_button.png"
WinPhone="Assets/close_circle_button.png"/>
</Image.Source>
</Image>
</ContentView>
</AbsoluteLayout>
</ScrollView>
I just tried with a fresh project and below is how it looks like for me. Close button is always behind the frame. here you can find repro test project
Removed Frame and moved some attributes onto StackLayout. Related bug
https://bugzilla.xamarin.com/show_bug.cgi?id=55744
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:test"
x:Class="test.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="EntryStyle" TargetType="Entry">
<Setter Property="PlaceholderColor" Value="#9cdaf1"/>
<Setter Property="TextColor" Value="#7dbbe6"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<ScrollView
HorizontalOptions="Center"
VerticalOptions="Center">
<AbsoluteLayout>
<StackLayout Margin="15" BackgroundColor="White"
IsClippedToBounds="True"
Padding="10, 5"
Spacing="3">
<Image
HorizontalOptions="Center"
x:Name="OctocatImage"
Margin="10"
HeightRequest="150"
WidthRequest="150">
<Image.Source>
<OnPlatform
x:TypeArguments="ImageSource"
Android="icon.png"
/>
</Image.Source>
</Image>
<Entry
HorizontalOptions="Center"
x:Name="UsernameEntry"
Style="{StaticResource EntryStyle}"
Placeholder="Username" />
<Entry
HorizontalOptions="Center"
x:Name="PasswordEntry"
Style="{StaticResource EntryStyle}"
Placeholder="Password"
IsPassword="True"/>
<Button
Margin="10, 5"
BackgroundColor="#7dbbe6"
HorizontalOptions="Fill"
x:Name="LoginButton"
Text="Login">
<Button.HeightRequest>
<OnPlatform x:TypeArguments="x:Double" Android="50" iOS="30" WinPhone="30"/>
</Button.HeightRequest>
</Button>
</StackLayout>
<ContentView
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds="1, 0, -1, -1">
<Image
x:Name="CloseImage"
HeightRequest="30"
WidthRequest="30">
<Image.Source>
<OnPlatform
x:TypeArguments="ImageSource"
Android="close_circle_button.png"
/>
</Image.Source>
</Image>
</ContentView>
</AbsoluteLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
It's a bug, set HasShadow="False" on your Frame and it should work.

Resources