Search code examples
c#androidxamarinseekbar

Setting up a SeekBar programmatically


for a past few hours I'm trying to set a width of a SeekBar by code (it has to be set by the configuration file). I was trying to set it via "seekBar[i].SetMinimumWidth(600);", I was also trying to do this with "ShapeDrawable", but it wasn't working, here is my example how I was doing that:

SeekBar sb = new SeekBar(this);
            sb.SetX(720 / 2);
            sb.SetY(1280 / 2);
            ShapeDrawable thumb = new ShapeDrawable(new OvalShape());
            thumb.SetIntrinsicHeight(64);
            thumb.SetIntrinsicWidth(600);
            sb.SetThumb(thumb);
            sb.SetBackgroundColor(new Color(255, 255, 255)); 

I was also thinking about some kind of style, but I'm not sure that this is possible to do this this way. How do I set a width of a seekbar by code?

I also have to set the min and max value of SeekBar, but i have no idea how to set those properites with code. I know, that I can simply lock it by if statment like this:

sb.ProgressChanged += (object sender, SeekBar.ProgressChangedEventArgs e) =>
                {
                    if (e.FromUser)
                    {
                       if (e.Progress < localSliderMax && e.Progress > localSliderMin)
                         //SomeWork
                    }
                };

But what if i will need for example a min value a -1, or max 255? It won't work like this. Any solution how to do this?


Solution

  • How do I set a width of a seekbar by code?

    You can set it for example like this:

    sb.LayoutParameters = new ViewGroup.LayoutParams(900, ViewGroup.LayoutParams.WrapContent);
    

    900 here in the code is the width of SeekBar. Please refer to the ViewGroup.LayoutParams Class for more information about the layout of control.

    But what if i will need for example a min value a -1, or max 255?

    You can't define the minimum value to -1. It is 0.

    To set 255, you can set the value to Max property of your SeekBar, here is a sample:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
    
        RelativeLayout mlayout = new RelativeLayout(this)
        {
            LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
        };
    
        SeekBar sb = new SeekBar(this)
        {
            LayoutParameters = new ViewGroup.LayoutParams(900, ViewGroup.LayoutParams.WrapContent),
            Max = 255,
        };
    
        mlayout.AddView(sb);
        SetContentView(mlayout);
        sb.ProgressChanged += Sb_ProgressChanged;
    }
    
    private void Sb_ProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
    {
        var progress = e.Progress;
        System.Diagnostics.Debug.WriteLine(progress);
    }
    

    To style the SeekBar, since there're too many possiblities, I don't know what exactly do you need, thus I only set Width and Max value from code behind which are specified in your question.