How to Customise the Alert OK / Cancel buttons using this aritchie/userdialogs in Xamarin Forms - xaml

Hi I am new to Xamarin Forms and am from native iOS development.
For showing alerts / action sheets I am using this https://github.com/aritchie/userdialogs.
For implementing this I followed this How to use Acr.UserDialogs.
I am getting alert successfully by following this, but now I need to customise the OK / Cancel Button's background color, alignment, frame values, hide and show the buttons..
Thanks in advance.

I need to customise the OK / Cancel Button's background color, alignment, frame values, hide and show the buttons.
As the author said, you could do this by creating a style and apply it in AlertConfig.
For example:
In the style.xml:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#AAAAAA</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#DDDDDD</item>
<!-- Used for the Alignment -->
<item name="android:gravity">center_horizontal</item>
</style>
And you could find this style Id in the Resource.Designer.cs.
// aapt resource value: 0x7f0b0189
public const int AlertDialogCustom = 2131427721;
Then in the code create a AlertConfig to config the alertdialog:
AlertConfig alertConfig = new AlertConfig();
alertConfig.OkText = "OKOK";
alertConfig.Message = "Message";
alertConfig.Title = "Title";
alertConfig.AndroidStyleId=2131427721;
UserDialogs.Instance.Alert(alertConfig);

With Rg.Plugins.Popup Nuget you can customize the popup.

Finally I am not using any nugget packages. Now I created my own CustomAlert class. I hope it will helpful for any one.
#Miguel Angel please look at below code
In my CustomAlert.xaml file
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="wwww/2009/xaml" x:Class="MY.UserControls.CustomAlert">
<Grid RowSpacing="0" ColumnSpacing="0">
<Grid BackgroundColor="#656565" Opacity="0.5">
</Grid>
<StackLayout BackgroundColor="{StaticResource WhiteColor}" VerticalOptions="Center" Margin="10" Padding="10" >
<Label Text="Title" FontSize="18" HorizontalTextAlignment="Center" Margin="0,0,0,0" TextColor="Black"></Label>
<Label Text="Descrption." FontSize="14"
HorizontalTextAlignment="Start"
Margin="10,10,5,5"
VerticalOptions="Center" TextColor="Gray"></Label>
<Button Text="OK" TextColor="{StaticResource WhiteColor}"
Command="{Binding DismissCustomAlertCommand}"
HorizontalOptions="Center" VerticalOptions="Center"
BackgroundColor="Red" Margin="30,0,30,0" WidthRequest="400" HeightRequest="40"></Button>
</StackLayout>
</Grid>
</ContentView>
Thank you

Related

HorizontalTextAlignment Element in a Maui xaml file, does not change the Alignment of the text

I am currently working on a UI for an application and I want to align the following label Text automatically horizontally, so I wanted to test, how I can align text normally.
The Label Documentation of Maui state, that I have to do it with the HorizontalTextAlignment Element. I tried it several times and it worked, but here it won't:
<CollectionView Grid.Row="1" BackgroundColor="Black">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type models:MessageModel}">
<models:MessageModel Message="Hallo" Created="01.01.2001 00:00:00"/>
<models:MessageModel Message="Hey na!" Created="01.01.2001 00:00:00"/>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="models:MessageModel">
<Label HorizontalTextAlignment="End" TextColor="White" Text="{Binding Created}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
The Output is the following (And yeah I could use one Label, but I wanted to try both ways):
Edit: adding the following Element HorizontalOptions="FillAndExpand" at the label + a Background Color:
Edit2:

Maui framework doesn't display sliders correctly

I have been dipping my toes in the .NET Maui framework, but unfortunately so far it has been quite disappointing. There seem to be multiple errors related to Shell (title view not working properly) and controls themselves. I have the following main page xaml file:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage">
<StackLayout Background="#111111" >
<Frame HasShadow="True" Margin="10" CornerRadius="10" BackgroundColor="#222222">
<StackLayout Spacing="30">
<Grid ColumnDefinitions="30,*">
<Label Text="R" FontSize="Medium" TextColor="White" />
<Slider GridLayout.Column="1" MinimumTrackColor="Red" ThumbColor="Red" MaximumTrackColor="#AAAAAA" />
</Grid>
<Grid ColumnDefinitions="30,*">
<Label Text="G" FontSize="Medium" TextColor="White" />
<Slider GridLayout.Column="1" MinimumTrackColor="#7FFF00" ThumbColor="#7FFF00" MaximumTrackColor="#AAAAAA" />
</Grid>
</StackLayout>
</Frame>
</StackLayout>
</ContentPage>
which is just a stack layout with a single frame that houses two sliders with labels. However, the two sliders seem to have a predefined width, as they are not filling the remaining space:
As soon as I add anything to any of the controls in the xaml while the app is running, for example a green background to the first grid, both sliders resize to the correct size:
However, when I reload the application with this change, the sliders go back to the wrong size:
There are additional issues that I encountered as well, when placing this content page as a flyout item in the shell where android behaviour was as described here, but additionally the Sliders are not interactable in Windows version. In this example with a clean project they do work in Windows version.
Try with any third-party packages like syncfusion, etc. to meet your requirements.

How to create circle and set image like screencast, I have used TintImage for this

Hi I am working in shoping app and design like, I have already used tint image:
How I can do that, I I can create a circel like this image
I am not sure why you are not using icons from the resources or get the images from rest api.
This is only needed if you want to create dynamic icons.
If that is the case, use a Frame and put the image on it.
<Frame CornerRadius="15" AbsoluteLayout.LayoutBounds="120, 10, 30, 30" Padding="5" VerticalOptions = "FillAndExpand" HasShadow= "false" BackgroundColor="red" >
<StackLayout Orientation="Vertical" Spacing="0" HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="1" VerticalTextAlignment="Center" HorizontalOptions="Center" />
</StackLayout>
</Frame>
the important thing to note is that , "cornerRadius" should be help the size of "AbsoluteLayout.LayoutBounds" width and size.
to learn more about AbsoluteLayout go here
Use FFImageLoading NuGet Library in your Solution, you can download using link FFImageLoading
Paste Below Code
<ffimageloading:CachedImage HorizontalOptions="Center" VerticalOptions="Center"
WidthRequest="300" HeightRequest="300"
DownsampleToViewSize="true"
Source = "http://loremflickr.com/600/600/nature?filename=simple.jpg">
<ffimageloading:CachedImage.Transformations>
<fftransformations:RoundedTransformation Radius="50"/>
</ffimageloading:CachedImage.Transformations>
</ffimageloading:CachedImage>

Xamarin forms label positioning

Newbie here. Am adding a label on the top of a page using XAML & Xamarin, but the label is too near the top of the page, so it's obscured by the iPad current time.
XAML code being used is:
<?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="XamlSamples.HelloXamlPage"
Title="XAML + Code Page">
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="#000000" Spacing="0" Padding="-300,-1,-300,29">
<Label Text="Text here"
Font="Large"
TextColor="White"
HorizontalOptions="Center"
VerticalOptions="Center"
Style="{DynamicResource TitleStyle}"/>
</StackLayout>
</ContentPage>
Take a look on you Stack Padding (Padding="-300,-1,-300,29") it's a little weird,
Also, you can add some layout with a padding before
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="#000000" Spacing="0" Padding="-300,-1,-300,29">
<Grid Padding="20 "/>
<Label Text="Text here"
Font="Large"
TextColor="White"
HorizontalOptions="Center"
VerticalOptions="Center"
Style="{DynamicResource TitleStyle}"/>
Change your padding to
Padding="x, 20, y, z"
In iOS, the height of the status bar is 20 pixels and is included when determining the position on the page (meaning a y location of y is at the top of the status bar and similarly the page). Windows and Android do not include the height of the status bar when determining the location on the page (meaning a y location of 0 is at the bottom of the status bar). It should also be noted that in some situations the status bar may actually be 40 pixels tall (in a call from what I've read).
Suggested use for a cross-platform option is to do something like this:
< ContentPage.Padding >
<!-- set platform - specific padding
iOS needs an additional
20px top padding to account for
iPhone status bar -->
< OnPlatform x:TypeArguments ="Thickness"
iOS ="0, 20, 0, 0"
Android ="0, 0, 0, 0"
WinPhone ="0, 0, 0, 0" />
</ ContentPage.Padding >
Note: this padding is being added to your ContentPage tag rather than the StackLayout tag. Code retrieved from here.

How to change ListView selected item's background color?

My ListView structure in xaml file is written below. In this listview, I am adding some list items. When I click on any item from this items the background color of selected item will be orange by default. I wish this selected iem's background to be black. How can I achieve this in xaml?
<ListView x:Name="LstMenu"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical">
<StackLayout Orientation="Vertical"`enter code here`
Padding="10">
<Label Text="{Binding ListItems}"
FontSize="15"
TextColor="White"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center" />
</StackLayout>
<Grid BackgroundColor="Black"
HeightRequest="1"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I really wish there was a great cross-platform solution to this issue, but to date I haven't found one. Here's a link to a nearly identical question from the Xamarin.Forms Forums. Using an Android project as an example, I typically achieve the effect you are looking for by adding a styles.xml file to the Resources directory:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<color name="CustomHighlight">#android:color/transparent</color>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">#drawable/androidsplash</item> <!-- must be lowercase -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
<style name="CustomTheme" parent="android:Theme.Holo">
<item name="android:icon">#android:color/transparent</item>
<item name="android:colorPressedHighlight">#color/CustomHighlight</item>
<item name="android:colorLongPressedHighlight">#color/CustomHighlight</item>
<item name="android:colorFocusedHighlight">#color/CustomHighlight</item>
<item name="android:colorActivatedHighlight">#color/CustomHighlight</item>
<item name="android:activatedBackgroundIndicator">#color/CustomHighlight</item>
<!--<item name="android:colorActivatedHighlight">#android:color/transparent</item>-->
</style>
<style name="ProgressBarStyle" parent="#android:style/Widget.ProgressBar.Horizontal"/>
</resources>
Note that I am using a sytem defined color (transparent) in this case, but you could reference or define any color you like.Then add a reference to the style in your MainActivity.cs file:
[Activity(Label = Constants.CompanyName,
Theme = "#style/CustomTheme",
Icon = "#drawable/icon",
MainLauncher = false,
ScreenOrientation = ScreenOrientation.Portrait,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
// your code here
}
I don't have a handy sample for Windows or iOS, but the concept is the same; override a default style in the platform specific project.