How to change ListView selected item's background color? - xaml

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.

Related

Two titleviews in dotnet maui

Im trying to create a Maui application with a dynamic Tabbar which should also be navigateable (and thus consist of NavigationPages). When I switch to a different tab, everything works fine, but as soon as is switch to an alreay visited tab, two titlebars appear:
One with the style i have defined (upper) and a default titlebar (lower). How can I remove the default titlebar?
Important code:
App.xaml.cs:
MainPage = new NavigationPage(new AppShell());
Page1:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:foo"
xmlns:converters="clr-namespace:foo.Converters"
x:Class="foo.MainPage"
NavigationPage.HasBackButton="False"
Title="foo"
Loaded="ContentPage_Loaded">
-- Content --
</ContentPage>
Page2:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="foo.Views.fooPage"
Title="foo">
-- Content --
</ContentPage>
NavigationbarStyle:
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="foo.Resources.Styles.NavigationbarStyle">
<Style TargetType="NavigationPage">
<Setter Property="BarBackground" Value="{DynamicResource NavigationBarBackgroundColor}"/>
<Setter Property="BarTextColor" Value="White"/>
</Style>
</ResourceDictionary>
AppShell (more tabs are added later on):
<Shell
x:Class="foo.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:foo"
xmlns:views="clr-namespace:foo.Views"
Shell.FlyoutBehavior="Disabled">
<TabBar x:Name="MainTabBar">
<ShellContent Title="Home"
Icon="alert_icon_2_neg.png"
ContentTemplate="{DataTemplate views:StartPage}">
</ShellContent>
</TabBar>
</Shell>

Xamarin.Forms DynamicResource doesn't seem to be working

I can't seem to get a DynamicResource working correctly.
Here's the XAML in App.xml:
<Application.Resources>
<ResourceDictionary>
<Thickness x:Key ="DefaultInsets"
Bottom="4"
Top ="4"
Left ="8"
Right ="8" />
</ResourceDictionary>
</Application.Resources>
And here's what I'm trying to do inside another file's ContentView:
<StackLayout
VerticalOptions="Center"
HeightRequest="225"
Margin="{DynamicResource DefaultInsets}" >
<Entry
x:Name="nameControl"
Placeholder="Full Name"
Margin="{DynamicResource DefaultInsets}"
/>
<Entry
x:Name="passwordControl"
IsPassword="True"
Placeholder="Password"
Margin="{DynamicResource DefaultInsets}" />
</StackLayout>
It seems like this should make consistent insets for the StackLayout from the ContentView, and for the Entry fields from the StackLayout. But I don't see any of that.
What have I done wrong?
Update : I have also tried this with StaticResource, and still no luck.
I use following type with ResourceDictionary. For distinguish, I set the top_margin to 40, here is my screenshot.
Here is my ResourceDictionary.
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="OuterPadding" x:TypeArguments="Thickness">
<On Platform="Android">8 ,40,8,4</On>
<On Platform="iOS">20</On>
<On Platform="WinPhone">24</On>
</OnPlatform>
<!-- left, top, right, and bottom-->
</ResourceDictionary>
</Application.Resources>
Here is my layout.
<StackLayout
VerticalOptions="Center"
HeightRequest="225"
Margin="{DynamicResource OuterMargin}" >
<Entry
x:Name="nameControl"
Placeholder="Full Name"
Margin="{DynamicResource OuterMargin}"
/>
<Entry
x:Name="passwordControl"
IsPassword="True"
Placeholder="Password"
Margin="{DynamicResource OuterMargin}" />
</StackLayout>
Update
I found you layout issue is related to the VerticalOptions="Center" in the StackLayout(If you set the VerticalOptions="Center", then set the margin value is not work).
If I delete it and use StaticResource.
Here is running screenshot(For testing, I change the value of margin top to 20)
Other answers may be valid for some people, but in the end here's what I was doing wrong:
InitializeComponent() was never getting called inside App.xaml.cs.
I had an if-then statement that was always hitting a return before ever getting to InitializeComponent().
When that doesn't get called, App.xaml is inaccessible to the rest of the app, and nothing defined there is available as a DynamicResource.
So I moved InitializeComponent() to happen before the if-then statement, and now it all works like gangbusters.
I hope this helps someone!

Why the "Hamburger" is not showing? I want to be able to access the Menu page by clicking on the hamburger icon

This is the code I have written to create the Master Detail Page:
Please check it out
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DemoApp.MenuPage" xmlns:local="clr-namespace:DemoApp.Views" MasterBehavior="Default">
<MasterDetailPage.Master>
<ContentPage Title="MenuPage" Icon="menuIcon.png" Padding="0,50,0,0">
<ContentPage.Content>
<StackLayout VerticalOptions="Start">
<Button Text="Home" />
<Button Text="Login" />
<Button Text="Logout" />
<Button Text="Exit" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<local:Login/>
</MasterDetailPage.Detail>
</MasterDetailPage>
From Xamarin 4.0, this MasterDetailPage feature can implemented much easier using Shell. David has made a video on showing how to use Shell. His Youtube video
I've checked your code in a plain project and no errors so far, to check further the Detail page code would be needed. I mean the content of
<MasterDetailPage.Detail>
<local:Login/>
</MasterDetailPage.Detail>

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

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

List<Color> in Xaml

I have my List defined in Xaml like this.
<ContentPage.Resources>
<ResourceDictionary>
<local:FileName x:Key="fileName">
<Color>#3599B8</Color>
<Color>#374649</Color>
<Color>#FD625E</Color>
<Color>#F2C80F</Color>
</local:FileName>
</ResourceDictionary>
</ContentPage.Resources>
FileName is defined in code behind like this.
public class FileName : List<Color>
{
}
Instead of directly setting the Color values, i want to define it as resource like this
<Color x:Key="BasicColorSchemeBlue">#3599B8</Color>
and use it.
Any suggestions on how to do this.
Thanks in advance.
GradientColors is an array of Colors.
<?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:GradientTest"
xmlns:s="clr-namespace:System;assembly=mscorlib"
x:Class="GradientTest.GradientTestPage">
<StackLayout Padding="20, 40, 20, 20">
<local:GradientViewRender HorizontalOptions="Center"
WidthRequest="300"
HeightRequest="50"
x:Name="gradientView">
<local:GradientViewRender.GradientColors>
<x:Array Type="{x:Type Color}">
<Color>#5FC900</Color>
<Color>#0FF2C8</Color>
</x:Array>
</local:GradientViewRender.GradientColors>
</local:GradientViewRender>
</StackLayout>
</ContentPage>
I have done like this on a working App.
This goes inside App.xaml:
<Color x:Key="COLOR_NAME">#ffffff</Color>
to access the color from a .cs file, use:
(Color)ResourceFinder.FindResource("COLOR_NAME");
Or use StaticResource or DynamicResource within an xaml file.