Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Floating Action Button in Xamarin Forms

Tags:

I'm building a Xamarin CrossPlatform App!

I wanted to add a floating action button at the bottom right corner of the app page just like this

https://i.stack.imgur.com/4PJcv.jpg

Here is my XAML code:

 <?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="Last_MSPL.Views.HomePage">       <ListView x:Name="Demolist" ItemSelected="OnItemSelected" BackgroundColor="AliceBlue">         <ListView.ItemTemplate>             <DataTemplate>                 <ViewCell>                     <ViewCell.ContextActions>                         <MenuItem x:Name="OnMore" Clicked="OnMore_Clicked" CommandParameter="{Binding .}"                                  Text="More" />                         <MenuItem x:Name="OnDelete" Clicked="OnDelete_Clicked" CommandParameter="{Binding .}"                                  Text="Delete" IsDestructive="True" />                     </ViewCell.ContextActions>                     <StackLayout>                          <StackLayout Padding="15,0">                             <Label Text="{Binding employee_name}" FontAttributes="bold" x:Name="en"/>                             <Label Text="{Binding employee_age}"/>                         </StackLayout>                      </StackLayout>                  </ViewCell>             </DataTemplate>         </ListView.ItemTemplate>     </ListView>    </ContentPage> 

How can I do this using XAML? Help me through this, Thanks!

like image 851
Hashir Malik Avatar asked Feb 04 '19 14:02

Hashir Malik


People also ask

What is float action button?

A floating action button (FAB) is a circular button that triggers the primary action in your app's UI. This page shows you how to add the FAB to your layout, customize some of its appearance, and respond to button taps.

How do I use absolute layout in xamarin?

An AbsoluteLayout is used to position and size children using explicit values. The position is specified by the upper-left corner of the child relative to the upper-left corner of the AbsoluteLayout , in device-independent units. AbsoluteLayout also implements a proportional positioning and sizing feature.


1 Answers

You can use an ImageButton (Xamarin.Forms v3.4+)

Create your image with a transparent background in your favorite editor and then assign it a location on the Page.

enter image description here

Example using an AbsoluteLayout, just place your "FAB" as the last element so that its Z-order is highest and it will "float" above the rest of the content.

    <AbsoluteLayout>           ~~~~          <ImageButton Source="editFAB.png"              BackgroundColor="Transparent"             AbsoluteLayout.LayoutFlags="PositionProportional"               AbsoluteLayout.LayoutBounds=".95,.95,80,80"              Clicked="Handle_Clicked" />     </AbsoluteLayout> 

Output:

enter image description here

like image 179
SushiHangover Avatar answered Oct 05 '22 05:10

SushiHangover