title: Implementing a slider (SeekBar) in Android
body: <p>I want to implement a slider, which is basically two lines, one vertical and one horizontal, crossing where the screen is touched. I have managed to make one but I have to issues:</p>
  
  <ol>
  <li>The slider is not very smooth, there is a slight delay when I'm moving the finger</li>
  <li>If I place two sliders it is not multitouch, and I'd like to use both of them simultaneously</li>
  </ol>
  
  <p>Here is the code: </p>
  
  <pre><code>public class Slider extends View {
  
      private Controller controller = new Controller();
      private boolean initialisedSlider;
      private int sliderWidth, sliderHeight;
      private Point pointStart;
      private Paint white;
      private int mode;
  
      final static int VERTICAL = 0, HORIZONTAL = 1, BOTH = 2;
  
      public Slider(Context context) {
          super(context);
          setFocusable(true);    
          // TODO Auto-generated constructor stub
      }
      public Slider(Context context, AttributeSet attrs) {
          super(context, attrs);     
          setFocusable(true);    
          pointStart = new Point();
          initialisedSlider = false;   
          mode = Slider.BOTH;
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          if(!initialisedSlider) {
              initialisedSlider = true;
              sliderWidth = getMeasuredWidth();
              sliderHeight = getMeasuredHeight();
  
              pointStart.x = (int)(sliderWidth/2.0);
              pointStart.y = (int)(sliderHeight/2.0);
              controller = new Controller(pointStart, 3);
  
              white = new Paint();
              white.setColor(0xFFFFFFFF);
          }
  
          canvas.drawLine(controller.getCoordX(),0,
                          controller.getCoordX(),sliderHeight, 
                          white);
          canvas.drawLine(0, controller.getCoordY(), 
                          sliderWidth, controller.getCoordY(), 
                          white);
  
      }
  
      public boolean onTouchEvent(MotionEvent event) {
          int eventaction = event.getAction();     
          int X = (int)event.getX(); 
          int Y = (int)event.getY(); 
          switch (eventaction) { 
          case MotionEvent.ACTION_DOWN:
              if(isInBounds(X,Y)) {
                  updateController(X, Y);
              }
              break;
          case MotionEvent.ACTION_MOVE:
              if(isInBounds(X,Y)) {
                  updateController(X, Y);
              }
              break;
          case MotionEvent.ACTION_UP:
              if(isInBounds(X,Y)) {
                  updateController(X, Y);
              }
              break;
          }
          invalidate();  
          return true; 
      }
  
      private boolean isInBounds(int x, int y) {
          return ((x&lt;=(sliderWidth)) &amp;&amp; (x&gt;=(0)) 
                   &amp;&amp; (y&lt;=(sliderHeight)) &amp;&amp; (y&gt;=(0)));
      }
      private void updateController(int x, int y) {
          switch(mode) {
          case Slider.HORIZONTAL:
              controller.setCoordX(x);
              break;
          case Slider.VERTICAL:
              controller.setCoordY(y);
              break;
          case Slider.BOTH:
              controller.setCoordX(x);
              controller.setCoordY(y);
              break;
          }
      }
  
      private class Controller {
          private int coordX, coordY;
          Controller() {
  
          }
          Controller(Point point, int width) {
              setCoordX(point.x);
              setCoordY(point.y);
          }
          public void setCoordX(int coordX) {
              this.coordX = coordX;
          }
          public int getCoordX() {
              return coordX;
          }
          public void setCoordY(int coordY) {
              this.coordY = coordY;
          }
          public int getCoordY() {
              return coordY;
          }
      }
  }
  </code></pre>
  
  <p>And the XML file: </p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello" /&gt;
      &lt;com.android.lasttest.Slider 
          android:id="@+id/slider"
          android:layout_width="100dp" 
          android:layout_height="100dp" 
          android:layout_gravity="center_horizontal" 
          android:adjustViewBounds="true"/&gt;
      &lt;com.android.lasttest.Slider 
          android:id="@+id/slider"
          android:layout_width="150dp" 
          android:layout_height="150dp" 
          android:layout_gravity="center_horizontal" 
          android:adjustViewBounds="true"/&gt;
      &lt;com.android.lasttest.Slider 
          android:id="@+id/slider"
          android:layout_width="200dp" 
          android:layout_height="200dp" 
          android:layout_gravity="center_horizontal" 
          android:adjustViewBounds="true"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>