6

How can I rotate the RatingBar? Is there an example for a vertical custom RatingBar?

N West
  • 6,768
  • 25
  • 40
Oli
  • 1,407
  • 3
  • 30
  • 47

2 Answers2

6

This is sample code for vertical rating bar...

public class VerticalRatingBar extends RatingBar {
    private int x, y, z, w;

    @Override
    protected void drawableStateChanged() {
        // TODO Auto-generated method stub
        super.drawableStateChanged();
    }

    public VerticalRatingBar(Context context) {
        super(context);
    }

    public VerticalRatingBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
        this.x = w;
        this.y = h;
        this.z = oldw;
        this.w = oldh;
    }

    @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;
        }

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            setSelected(true);
            setPressed(true);
            break;
        case MotionEvent.ACTION_MOVE:
            setProgress(getMax()
                    - (int) (getMax() * event.getY() / getHeight()));
            onSizeChanged(getWidth(), getHeight(), 0, 0);

            break;
        case MotionEvent.ACTION_UP:
            setSelected(false);
            setPressed(false);
            break;

        case MotionEvent.ACTION_CANCEL:
            break;
        }
        return true;
    }

    @Override
    public synchronized void setProgress(int progress) {

        if (progress >= 0)
            super.setProgress(progress);

        else
            super.setProgress(0);
        onSizeChanged(x, y, z, w);

    }
}
Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
  • I tried with this code, exactly as It's written but It didn't work, could be because I'm using a custom style in my RatingBar? – 4gus71n Dec 01 '15 at 20:34
0

There's a setRotation method on the view which will hopefully achieve what you're after.

BDFun
  • 531
  • 2
  • 7
  • This exists since API Level 11. What about older versions? – Oli Dec 15 '11 at 10:45
  • You could try extending the RatingBar so you can override the onDraw method: protected void onDraw(Canvas c) { c.rotate(90); super.onDraw(c); } – BDFun Dec 15 '11 at 11:48