Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the text in EditText vertically (Android)

(Sorry for my poor question. I have update it now)

How can i make it in XML file? I tried to use following code, but not correct (I used "android:rotation="-90" to do rotation.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="141dp"
    android:layout_height="200dp"
    android:layout_weight="0.41"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/sidebar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shape_card_sidebar"
        android:inputType="text"
        android:rotation="-90"
        android:text="I want to be like this" >
    </EditText>
</FrameLayout>

enter image description here

like image 826
LiangWang Avatar asked Apr 19 '13 13:04

LiangWang


Video Answer


1 Answers

You're going to run into several problems if you try to do it that way. The most obvious issue will be the incorrect measurement. Instead you should create a custom view. Something like this:

public class RotatedTextVew extends TextView {
    public RotatedTextView(Context context) {
        super(context);
    }

    public RotatedTextView(Context context, AttributeSet attrs) {
        super(context, attrs)
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Switch dimensions
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(90);
        super.onDraw(canvas);
        canvas.restore();
    }
}

I haven't actually tested this, but this is how I'd start.

like image 66
Michael Pardo Avatar answered Sep 28 '22 03:09

Michael Pardo