Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change height of progress bar in Xamarin Forms?

How do I change the height of a Xamarin Forms ProgressBar in code? Am using Xamarin Forms V2.1.

.HeightRequest and .MinimumHeightRequest seem to have no effect. The default progress bar is so thin that it might not even be noticed.

.BackgroundColor does not seem to work either.

What am I missing here?

like image 270
BillF Avatar asked Apr 16 '16 05:04

BillF


2 Answers

You need custom renderers for this:

First create a class for your custom progress bar:

public class CustomProgressBar :ProgressBar
{
  public CustomProgressBar()
   {
   }
}

And then also add a new file for your custom progress bar renderer:

For iOS:

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(
     ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        Control.ProgressTintColor = Color.FromRgb(182, 231, 233).ToUIColor();// This changes the color of the progress
    }


    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        var X = 1.0f;
        var Y = 10.0f; // This changes the height

        CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
        Control.Transform = transform;
    }
}

[EDIT: fixed code above as per comment]

For Android:

public class CustomProgressBarRenderer :ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);


        Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid()); //Change the color
        Control.ScaleY = 10; //Changes the height

    }
}

I hope this helps you!

like image 152
KaeM Avatar answered Sep 29 '22 10:09

KaeM


To make the progress bar thicker, you just have to change the ScaleY property of the ProgressBar.

For example: This code

<ProgressBar Progress=".5"/>
<ProgressBar ScaleY="2" Progress=".5"/>
<ProgressBar ScaleY="5" Progress=".5"/>

Produces this enter image description here

Note that you may need to adjust your margins accordingly.

like image 28
Bedir Avatar answered Sep 29 '22 11:09

Bedir