How to Rotate TextView 90 Degrees and display [duplicate]

Solution 1:

Try this::

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:rotation="-95"
                android:text="2" 
                />

Solution 2:

The fastest and most convenient way is to Rotate by Animation

use rotate animation on your regular TextView like so.

rotateAnimation.xml:

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
           android:fromDegrees="0" 
           android:toDegrees="-90"
           android:pivotX="50%"
           android:duration="0"
           android:fillAfter="true" />

Java Code:

  TextView text = (TextView)findViewById(R.id.txtview);       
  text.setText("rotated text here");

  RotateAnimation rotate= (RotateAnimation)AnimationUtils.loadAnimation(this,R.anim.rotateAnimation);
  text.setAnimation(rotate);

Solution 3:

In android for any new view there is a method called setRotation(float) you can use it

textview.setRotation(float);

but please note that this method is Added in API level 11

so if you want to support it you can use this

if (Build.VERSION.SDK_INT < 11) {

    RotateAnimation animation = new RotateAnimation(oldAngel, newAngel);
    animation.setDuration(100);
    animation.setFillAfter(true);
    watermarkText.startAnimation(animation);
} else {

    watermarkText.setRotation(progress);
}

Solution 4:

 <TextView 
            android:id="@+id/rotated_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:alpha=".3"
            android:background="@drawable/grey_capsule"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textSize="14sp"
            android:padding="4dp"
            android:layout_marginLeft="-50dp"
            android:rotation="-90"
            android:text="Andrew Coder" />

Solution 5:

[SOLVED] After a year of this being on my bucket list without any suitable answers on the forums I have finally sorted this!

The trick here is to hard-code the layout_width and layout_height of the TextView larger than the text will ever be - e.g. 100dp x 100dp.

Then place the TextView in a FrameLayout and turn clipping off for the FrameLayout android:clipChildren="false" and clip to padding off for the TextView android:clipToPadding="false":

TextView with hard-coded height and width

The TextView will now float in the FrameLayout. Use the TextView gravity settings to move the Text within it's boundary.

Then use the layout_gravity settings to move the boundary within the parent FrameLayout within it:

Here is an example of right and bottom aligned text rotated through -90 degrees:

Solution example

[UPDATE] Example XML for a frame view housing rotated text:

       <FrameLayout
            android:layout_width="@dimen/item_col_width_val"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:padding="@dimen/table_header_margin">
            <TextView
                android:layout_width="@dimen/table_title_row_height"
                android:layout_height="@dimen/table_title_row_height"
                android:layout_gravity="bottom|end"
                android:gravity="bottom"
                android:rotation="-90"
                android:text="@string/asm_col_title_in_stock"
                android:textColor="@color/colorGood" />
        </FrameLayout>