0

I am trying to make an Android application with two seekbars that can be controlled at the same time (multi-touch). The full source code can be found here

The app is a robot controller, each seekbar controls a motor so to move forward you slide both bars up and so on (speed is controlled by how far you push it up or down) where 50 is the stop state, 100 is the maximum speed forward and 0 is maximum speed backward.

What I am trying to implement is that after you let go of the seekbar it will go back to the stop state (progress 50), however, even though the value is actually set to 50 (using setProgress) but the view won't update to reflect that. I tried using .invalidate(), .requestLayout() and some old solutions here and here but couldn't get it to work.

Here are parts of the code: (for full code check the github link above)

public class MainActivity extends ActionBarActivity {
    ...

    ...
    public static void ResetSeekBar(View v) {
        if ( v.getId() == R.id.sb1) {
            Log.d("before", "Progress = " + Integer.toString(sb1.getProgress()));
            sb1.setProgress(50);
            Log.d("after", "Progress = " + Integer.toString(sb1.getProgress()));
            sb1.invalidate();
            //sb1.requestLayout();

        } else if ( v.getId() == R.id.sb2) {
            sb2.setProgress(50);

        }
    }
}


public class MySeekBar extends SeekBar {
    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public boolean onTouchEvent(final MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            Log.d("tag", "UP");
            MainActivity.ResetSeekBar(this);
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            Log.d("tag", Integer.toString(getProgress()));
            MainActivity.UpdateText(this, Integer.toString(getProgress()));
        } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.d("tag", "DOWN ");
            MainActivity.UpdateText(this, Integer.toString(getProgress()));
        }
        return super.onTouchEvent(event);
    }
}

Using a regular button onClick works and do update the view but not after MotionEvent up. So what am I doing wrong?

Also how can I make it reset to 50 but with a fast transition instead of suddenly jumping to the middle?

Community
  • 1
  • 1
Fahad Alduraibi
  • 372
  • 6
  • 17

1 Answers1

0

Of course the solution comes after you ask poeple! anyways, it is much simpler than I thought.

super.onTouchEvent(event) should be called before processing the event, i think I was trying to change it before it actually processed the change, so it was overriding what i did! (not sure just guessing based on the behaviour that I saw)

and here is the function after the fix:

public boolean onTouchEvent(final MotionEvent event) {
    super.onTouchEvent(event);
    if (event.getAction() == MotionEvent.ACTION_UP) {
        Log.d("tag", "UP");
        MainActivity.ResetSeekBar(this);
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        Log.d("tag", Integer.toString(getProgress()));
        MainActivity.UpdateText(this, Integer.toString(getProgress()));
    } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
        Log.d("tag", "DOWN ");
        MainActivity.UpdateText(this, Integer.toString(getProgress()));
    }
    return true;
}
Fahad Alduraibi
  • 372
  • 6
  • 17