Forum Xamarin.Android

Seekbar Thumbs behaving strange at extreme points

I'm implementing a seekbar in my current android application and it was working perfectly. The thumb image is fairly wide (300dp) compared to the background image of the seekbar (which is 600dp) and the thumb "snaps" into place if you go above or below certain threshold values on the bar(which has a maximum progress value of 300). However, when I wanted to change the thumb image for the "snapped" (same basic image but different colours) the thumb started behaving oddly. It started moving "faster" than the finger and it's edges stopped far outside of the bar even though the progress values were the same. By searching a bit online and in the API I found out that the "SetThumb" method changed the ThumbOffset to half the size of the thumb which seemed to be the reason for the strange behaviour. When I checked by printing seekBar.ThumbOffset to the console the value didn't change however. I still tried setting the offset to its value before the image change every time which to my surprise actually worked. Almost...

The thumb behaved as it should almost all the time, except for when pulled to the extreme values before letting go. When this happens it goes back to those weird positions halfway out of the seekBar. I have no idea why this is happening since the same code should be in place no matter if the progress value is 299 or 300...

Any help or suggestion would be appreciated!

Here's the code in question:

private class SeekBarListener : Java.Lang.Object, SeekBar.IOnSeekBarChangeListener
{
    TextView tvQuestion = CurrentStats.GetCurrentView ();
    int position;

    public SeekBarListener(int pos)
    {
        this.position = pos;
    }

    public void OnProgressChanged(SeekBar seekBar, int progress, bool fromUser)
    {
        if (progress < 3) {
            seekBar.Progress = 3;
            progress = 3;
        } else if (progress > 297) {
            seekBar.Progress = 297;
            progress = 297;
        }
        tvQuestion.SetX (progress);
    }

    public void OnStartTrackingTouch(SeekBar seekBar)
    {
    }

    public void OnStopTrackingTouch(SeekBar seekBar)
    {
        int i;
        if (seekBar.Progress < 120) {
            i = seekBar.ThumbOffset;
            seekBar.SetThumb (CurrentStats.GetThumbBar(1));
            seekBar.ThumbOffset = i
            seekBar.Progress = 3;
            tvQuestion.SetX (seekBar.Progress);
            CurrentStats.SetQAnswer (false, position);
        } else if (seekBar.Progress > 180) {
            i = seekBar.ThumbOffset;
            seekBar.SetThumb (CurrentStats.GetThumbBar(2));
            seekBar.ThumbOffset = i;
            seekBar.Progress = 297;
            tvQuestion.SetX (seekBar.Progress);
            CurrentStats.SetQAnswer (true, position);
        } else {
            i = seekBar.ThumbOffset;
            seekBar.SetThumb (CurrentStats.GetThumbBar(0));
            seekBar.ThumbOffset = i;
            seekBar.Progress = 150;
            tvQuestion.SetX (seekBar.Progress);
            CurrentStats.ResetQAnswer (position);
        }
    }
}

Posts

  • KurodaniKurodani SEMember

    still not sure what the problem was, but kind of fixed it by removing the "seekBar.Progress = x" in the OnProgressChanged method. I used these to fix a minor graphical glitch while moving the thumb, but it's hardly noticable and definately preferable to the other problem...

Sign In or Register to comment.