2

I am developing an Android app using Xamarin and I'm looking for a solution of making restrictions on Android seekbars to prevent users from moving the seekbar to the right (increasing the value).

Currently, the page has 4 seekbars and each of them represent a number of Apple to be given to a person from "Basket" (Basket is shared amongst everyone). (A, B, C, D)

If "basket" has any values (basket is an int type), then users can move any of the sliders. For example, If they wish to give more apple to A then simply they can just move the first slider to the right.

IF "basket" doesn't have any values then none of the sliders should be able to move.

Because there are 4 seekbars (4 ppl) and it has to be synchronised (meaning that every change in the bar must be taken into an account as ppl share the same "basket")

Is there a way to disable seekbars from moving to the right?

Thanks In advance

Minkyu Ham
  • 21
  • 1
  • Have a look at [this answer](https://stackoverflow.com/questions/16284219/disable-changes-on-seekbar-by-client), try to find is there any `setEnabled` api or you have to override `onTouch` on seekbar. – sakiM Feb 11 '18 at 06:24

2 Answers2

1

Could this do the job?

seekBar.ProgressChanged += (object sender, SeekBar.ProgressChangedEventArgs e) => {
    if (e.FromUser < oldValue)
    {
        oldValue = e.Progress;
        //your stuff
    }
    seekBar.Progress = oldValue;
};
Hichame Yessou
  • 2,658
  • 2
  • 18
  • 30
0

Because there are 4 seekbars (4 ppl) and it has to be synchronised (meaning that every change in the bar must be taken into an account as ppl share the same "basket")

Is there a way to disable seekbars from moving to the right?

You need to check the total value when ProgressChanged and if overflows then set the current seekbar's progress to the max value:

public class MainActivity : Activity
{
    SeekBar sbOne, sbTwo, sbThree, sbFour;
    TextView tvTotal;
    int total=100;
    List<SeekBar> seekbars;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        sbOne = FindViewById<SeekBar>(Resource.Id.sbOne);
        sbTwo = FindViewById<SeekBar>(Resource.Id.sbTwo);
        sbThree = FindViewById<SeekBar>(Resource.Id.sbThree);
        sbFour = FindViewById<SeekBar>(Resource.Id.sbFour);
        tvTotal = FindViewById<TextView>(Resource.Id.tbTotal);
        seekbars = new List<SeekBar> { sbOne, sbTwo, sbThree, sbFour };

        //register the events
        for (int i = 0; i < 4; i++)
        {
            seekbars[i].ProgressChanged += ProgressChanged;
        }
        

    }
    

    private void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        SeekBar sb = sender as SeekBar;
        int tmp=0;
        for (int i = 0; i < 4; i++)
        {
            tmp += seekbars[i].Progress;
        }

        if (tmp > total)
        {
            //if overflows then set current seekbar's progress to a proper value
            sb.Progress-=(tmp-total);
        }
    }
    
}
Community
  • 1
  • 1
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24