Alright so today I’m gonna take you guys through a journey of decorating a Xamarin Forms Slider control in Android…
Well we all know how basic the default Slider control in Xamarin Forms, but worry not, there are plenty of ways to decorate it with awesome features,specifically for Xamarin Android! 😀
It all started a few weeks back when I was playing around with the Slider control to add a whole bunch of complex visual features in one of my company apps.
Let’s get started…
Alright, first thing first, we need set up our Custom Slider control first and attach it’s Custom renderer in Android.
So here goes the Custom Slider control subclassing in Xamarin Forms project…
Next set up the Custom Slider Renderer in your Android project…
How would I consume this in XAML you ask? Just as another ordinary view… 😉
Now keep in mind all the customization that we are doing later will be done within this custom renderer.
Something to keep in mind is that,
the Xamarin Forms Slider control’s Android run time native counterpart is the SeekBar…
So the more you aware of the SeekBar’s properties, the more customization you could play around with! 😉
Alright let’s start decorating… 😀
Decoration 1 : Change color theme!
Want to change the color theme of your Slider? Here we go…
Let’s see the code.
Use the ProgressDrawable Property and set the ColorFilter to it as you wish… 😉 which will set the color theme to your Progress Bar , Progress Thumb, and the background bar…
Decoration 2 : Change only the Slider’s Thumb Color?
How about changing just the Progress Thumb’s color? Yes you may…
Look at that funky looking Progress Thumb! 😉
Android SeekBar (which is the native handler of Xamarin Forms Slider on Android) has the Thumb property which allows you to customize the appearance of the little thumbnail head of the Slider control as we have used above.
Decoration 3 : Change progress background Color?
How about changing only the Progress Bar’s background color? As you can see below..
Look at the boring default progress bar’s background color vs the purple background color! 😉 pretty cool!
Here’s how you do it,
Use the ProgressBackground property to set the TintList and the TintMode! 🙂
Decoration 4 : How about adding a secondary progress indicator?
Now we all have seen secondary progress indicators in progress bars, specially in online video stream players… 🙂 example take the Youtube player! 😉 So have you ever wanted to add such a cool feature to your Xamarin Forms Slider in Android? Let me show you how its done… 😉
Look how cool that is yeah! 😉
Alright let’s get into coding…
Now in Android we have this built in property called SecondayProgress which allows you to set a secondary progress value to your Slider or Seekbar as of native Android handler.
There you go, you can see that we are setting the SecondaryProgress value, but also take a closer look at the calculation that we are doing before setting it.
Now Xamarin Forms Slider and Xamarin Android Seekbar which is the handler for the Slider control, uses different value types or unit types for setting the Progress and the Secondary Progress values in native level. If we want to set the value from Xamarin Forms value units then we need to convert that value to Android Seekbar’s native values which is exactly what we are doing at the calculation. So basically we are setting the Xamarin Forms unit value according to the native units to Seekbar control.
Oh if you want to set the Secondary Progress from Xamarin Forms level then you can easily create a property in the CustomSlider class and use it down here in your Custom Renderer class 🙂 Imagination is the limit! 😉
Decoration 5 : May be change the Color of secondary progress indicator?
How about we spice it up by changing the color a little of the secondary progress? 😉
Look at that!
Time for coding…
Android Seekbar has this property called SecondaryProgressTintList and SecondaryProgressTintMode which allows you to achieve the above results and set the secondary progress color as you wish…
And hey of course don’t forget to set the SecondaryProgress value while you’re at it!
Decoration 6 : I would call it Funky delight!
Alright, now all that being said, how about blending some of those different colors adding some funky delight-ness to the Slider? 😉
Well what I mean is, let’s try adding different color’s to Thumb, Progress Bar, Progress Bar background and Secondary Progress Bar!
Too much funky? I thought so!
How about these??? 😉
I know, I love playing with colors being a Visual oriented developer! 😀 lol
Your imagination is the limit fellas!
Here’s how you play around with the colors…
Decide your flavor of colors and go crazy fellas! 😉
Decoration 7 : Remove Thumb Header may be?
Absolutely, check this out…
It’s pretty simply actually, simply set a Tranparent ColorDrawable to the Thumb property.
Decoration 8 : Let’s kick it up a notch!
Let’s add some more vibrant and complex customization to our Slider for Android! 🙂
How about throwing in some cool gradient effects…
So to achieve that, we shall be using Android native Styling with Drawables such as Shape, Gradients and so on.
We will create a native android xml Style file in your Resources/Drawable folder with the name “custom_progressbar_style.xml”
Here’s what you’ll be placing inside of it…
So to explain the above a little bit, we have created a Style layer-list which assigns the given styling items to the specific id’s of the SeekBar native control, such as the background, secondaryprogress, progress as you have noticed. Those drawable objects will replace the default styles of those segments in the SeekBar with these defined drawable objects.
First we are using a Shape drawable to to the Background property, which creates a gradient layer with the given colors and angle for creating a gradient effect! Also we are setting a radius value to corner so that the corners will be curved nicely.
Next for the Progress and the SecondaryProgress properties we are creating a similar Shape gradient as before but we are clipping it according to the given values of each of them.
There’s how you set it in the custom renderer level, simply call the SetProgressDrawableTiled() method by passing the custom style of what we created above.
Also I have disabled the Thumb, just to make it look cooler. Its up to you though.
If you want to do more extensible customization like above and may be preserve the Thumb view and style that as well? then refer to this stackoverflow article: http://stackoverflow.com/questions/16163215/android-styling-seek-bar
Additionally you could have bitmap images or nine patch images as drawables to styling and so may other stuff.
Now this is like the holy grail.. where as you can see with Android you get full flexibility for any kind of complex customization!
Its only limited by your imagination! 😉
Decoration 9 : Can I reduce the above overridden Height?
So you’re worried of the height after setting the custom styling drawables as above? simply reduce the HeightRequest value in your Xamarin Forms code for our custom Slider view.
Right after we set the Custom ProgressDrawable styles in the renderer level, it overrides the Height property of native SeekBar that’s assigned from Xamarin Forms level for each of those sub-views (ProgressBar, SecondaryProgressBar and Background sub views), so they expands themselves to the fullest as possible with the new Drawable objects.
So the above should give you control over the Height issue!
Or else you could set the dip IntrinsicHeight values in your XML style drawables itself as well (something extra)! 😉
Decoration 9 : Can I have the above cool-ness programmatically without resources?
So you don’t like to deal with Android Resources and creating the Style XMLs and stuff?
Oh sure, no worries! but you will have to do a little bit of work to get the above simple XML Styling into pure code generated objects!
Let me begin by giving credit to this example written in Java which I found while I was in the same situation: FlatUI/FlatSeekBar.cs
So let’s see how we could create Drawable Style objects in C# code!
Now keep in mind all the Drawable objects we used in our XML file “custom_progressbar_style.xml” has their own programmatical counterparts such as Shape, Gradient and Clip by the names as ShapeDrawable, GradientDrawable, and ClipDrawable and so on likewise.
So we can convert any given XML style to a C# generated style drawable. (any native Android developer should be well aware of this)
So let’s do something similar! 😉
So we are going to create our own Drawable objects and set them to the sub-views of our Slider control for Android, just like how we did with the XML styling, but this time programmatically. Here is how it will look like…
There you haveit, let’s see how we did this…
So basically we are creating our our Drawable objects programatically, in this case PainDrawable objects and giving them different styling values such as Radius, Clipping, IntrinsicHeight and so on.
And then at the end we are going to retrieve the references for the sub-views of our native SeekBar in Android which is the after-rendering counterpart of Xamarin Forms Slider as I mentioned at the beginning. This is going to be a LayerDrawable object, which is going to allow us to set our own custom Drawable objects to each drawable layer by their IDs.
As you can see we are calling the SetDrawableByLayerId() on our LayerDrawable object and passing in the each sub-view reference and custom drawable objects we want to set to them. 😀
Now keep this in mind, here you could have any kind of drawable objects to create your custom drawable styling just like you previously did with GradientDrawable in XML style, have the exact same beautiful visual result rendered programatically! 🙂
Well fellas that’s it for now, well at least that’s all I came across while I was playing around with my Custom Renderer for Xamarin Forms Slider on Android! 😀
Enjoy and share!