Add a SnackBar to you Xamarin.Android app
Material design introduced a new lightweight way to provide feedback to a user, something that sits between the AlertDialog and Toast alternatiives with a funny name: SnackBar.
A Snackbar appears at the bottom of the view and can optionally display an additional custom button.
Here are the basic steps to show a SnackBar:
-Open Visual Studio and create a new blank Android app
-Using Nuget add the Xamarin.Android.Support.Design library, this will let you target Android releases older than v21
- -Using the code generated by the template, lets make the SnackBar appear when the default button is pressed.
-Add this code inside MainActivity’s OnCreate method
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById<Button>(Resource.Id.MyButton); button.Click += (s, e) => { //Creates the Snackbar Snackbar snackBar = Snackbar.Make(button, "Text", Snackbar.LengthIndefinite); //Show the snackbar snackBar.Show(); }; } }
- -Compile the project and wait until build succeeds, be patient if operation takes more than than usual since this delay is due to the fact that the missing packages are downloading and their size is quite huge. If you stop compilation download will fail and you’ll start having a lot of “…invalid zip” error. in this case delete the zip folder mentioned on error message and try again.
- -You’ll now see this error message:
- -Let’s fix it adding the required theme (you can create your own deriving from Theme.AppCompat of course)
[Activity(Label = "TestSnackBar2", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat")]
- -Run the project again and you’ll see this bar appear at the bottom of the view
- -Quite sad indeed, luckily the SnackBar can be customized, so let’s add more code:
button.Click += (s, e) => { //Creates the Snackbar and subscribes the button press event Snackbar snackBar = Snackbar.Make(button, "Text", Snackbar.LengthIndefinite).SetAction("Ok", (v) => { Console.WriteLine("Done"); }); //set action button text color snackBar.SetActionTextColor(Android.Graphics.Color.Green); //Set action button text size TextView txtAction = snackBar.View.FindViewById<TextView>(Resource.Id.snackbar_action); txtAction.SetTextSize(Android.Util.ComplexUnitType.Dip, 18); //Set message text size and color TextView txtMessage = snackBar.View.FindViewById<TextView>(Resource.Id.snackbar_text); txtMessage.SetTextColor(Android.Graphics.Color.Red); txtMessage.SetTextSize(Android.Util.ComplexUnitType.Dip, 12); //Show the snackbar snackBar.Show(); };
If you’re wondering why you need to pass a reference to a view (Button in our case) as SnackBar first parameter is because internally it walks up the visual tree so that can properly position itself at bottom position.
about me

Welcome! I'm Corrado, an Italian software consultant, Microsoft MVP, trainer and speaker and i use this space to post some thoughts about my daily working experiences. If you like blog contents just subscribe my rss feed.
Corrado's Blog 1.0
where i'm online
latest tweets
tags




- Recent Posts
- Recent Comments
- Ron - Xamarin Novice on Adding a splash screen to Xamarin Forms apps
- msdYqb on Xamarin Forms: CarouselView in action
- msdYqb on Adding a splash screen to Xamarin Forms apps
- Jose Cueva on Adding a splash screen to Xamarin Forms apps
- Ali on Xamarin.Android Status bar color not set
- Archives
- August 2018
- July 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- September 2016
- August 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- September 2015
- August 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- October 2014
- September 2014
- July 2014
- May 2014
- April 2014
- January 2014
- December 2013
- September 2013
- August 2013
- July 2013
- April 2013
- March 2013
- January 2013
- November 2012
- October 2012
- August 2012
- July 2012
- Categories
- Meta
- TagsAndroid Android emulator Binding Blend C# CarouselView Custom controls effects encoding Fiddler FloatingActionButton Genymotion german git Google GridView IDEA internet iOS ipconfig Lottie Material Design MVVM MvvmStack oAuth ParallaxView ScrollViewer Skype SnackBar Typescript UWP VirtualBox Visual Studio WebClient Win2D Windows 8 Windows Phone WinJS WinJS binding list WinRT Xamarin Xamarin.iOS Xamarin Forms XAML Xamrin