Simply rotating a horizontal (normal) seekbar with android:rotation="270"
makes the appearence rotate, but does not rotate it in the layout and the bounds stay horizontal. I have tried to extend the Seekbar class (Java, shown below) but the position callout and touch animation stays on the horizontal axis (image below). Even if they were in the correct positions, the callout would be pointing down instead of to the right. Am I missing something in my code or is there a better way to implement a discrete vertical seekbar?
Custom seekbar class:
import com.google.android.material.slider.Slider;
public class VerticalSeekbar extends Slider {
public VerticalSeekbar(@NonNull Context context) {
super(context);
}
public VerticalSeekbar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekbar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(),0);
super.onDraw(c);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) return false;
if(event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE
|| event.getAction() == MotionEvent.ACTION_UP) {
event.setLocation(getHeight() - event.getY(), event.getX());
return super.onTouchEvent(event);
}
return true;
}
}
xml:
<com.app.mechanismsimulator2.ui.widgets.VerticalSeekbar2
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="50dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:stepSize="1"
android:value="0"
android:valueFrom="-2"
android:valueTo="2" />