How to Rotate Textview 90 Degrees and Display

How to Rotate TextView 90 Degrees and display

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);

Rotate total Textview in -90 degrees

for a vertical TextView i used the following code and it worked perfectly.

public class VerticleTextView extends TextView {

final boolean topDown;
public VerticleTextView(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();
}
}

how to rotate a textView in android?

you can use rotate function in xml

android:rotation="-180"

for dynamic use

textview.setRotation(-180);

How to Rotate TextView?

Change the main.xml to :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_alignParentLeft="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="TextView" android:layout_marginTop="50dip"
android:layout_marginLeft="50dip" android:id="@+id/text1"></TextView>

</RelativeLayout>


Related Topics



Leave a reply



Submit