0

So basically I have tried each and every method given here to make a vertical textview. First is the 'rotation:"-90"'(XML TextView attribute) method which makes the textview vertical , but it still takes the same width as it has on normal mode. I have also tried this implementation given here.

But all these methods are not working as expected in androidX project.The modified Textview implementation also acts as a default one on rotating, and takes the same width as it have on horizontal/default mode.

1 Answers1

0

I achieved ,what I was trying to achieve. In androidX This implementation needs to be modified a little .Make the class extend AppCompatTextView rather than TextView .

import androidx.appcompat.widget.AppCompatTextView;


public class VerticalTextView  extends AppCompatTextView{
final boolean topDown;
public VerticalTextView(Context context, AttributeSet attrs){
    super(context, attrs);
    final int gravity = getGravity();
    if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
        setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
        topDown = false;
    }else
        topDown = true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}

@Override
protected void onDraw(Canvas canvas){
    TextPaint textPaint = getPaint();
    textPaint.setColor(getCurrentTextColor());
    textPaint.drawableState = getDrawableState();

    canvas.save();

    if(topDown){
        canvas.translate(getWidth(), 0);
        canvas.rotate(90);
    }else {
        canvas.translate(0, getHeight());
        canvas.rotate(-90);
    }


    canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

    getLayout().draw(canvas);
    canvas.restore();
}

}

Also ,this Implementaion does the same ,but the Textviews color can't be changed dynamically with this one.