Oya
Oya

Reputation: 903

How to make a vertical TextView

I am making an application in Android and I am currently trying to make a vertical TextView. I want it to look similar to this.

enter image description here

Does anyone know how to achieve this, or at least an existing library which I can use to get this effect.

Thanks

Upvotes: 3

Views: 2037

Answers (2)

amhdch
amhdch

Reputation: 11

You must create custom view

For example:

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 boolean setFrame(int l, int t, int r, int b) {
    return super.setFrame(l, t, l + (b - t), t + (r - l));
}

@Override
public void draw(Canvas canvas) {
    if (topDown) {
        canvas.translate(getHeight(), 0);
        canvas.rotate(90);
    } else {
        canvas.translate(0, getWidth());
        canvas.rotate(-90);
    }
    canvas.clipRect(0, 0, getWidth(), getHeight(), Region.Op.REPLACE);

    super.draw(canvas);
}
}

Upvotes: 1

Jay Rathod
Jay Rathod

Reputation: 11245

If you want vertical Text View like this then you need to pass \n to Text View text property.

Refer this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="#000"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="5dp"
        android:text="A\nB\nC\nD\n"
        android:textColor="#FFF"
        android:textSize="18dp" />

</RelativeLayout>

Upvotes: 3

Related Questions