1

I want to set my textview in -90 degrees means i haver to show it like this! i ahve to show 8 textviews in single linear layout. i am able to rotate but i can see only one textview.Please can any one help me with some example.

enter image description here

Thanks in advance.

public class AngledTextView extends TextView
{ 

       public AngledTextView(Context context, AttributeSet attrs) 
       { 
          super(context, attrs); 
       } 

       @Override 
       protected void onDraw(Canvas canvas) 
       { // Save the current matrix 
         canvas.save(); 
         // Rotate this View at its center 
         canvas.rotate(270,this.getWidth()/2, this.getHeight() /2); 
         // Draw it 
         super.onDraw(canvas); 
         // Restore to the previous matrix 
         canvas.restore(); 
        } 
 } 
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
user484848
  • 47
  • 1
  • 7
  • you should search here before asking,there are a lot of questions already answered [how to rotate text view](http://stackoverflow.com/questions/6813367/how-to-rotate-textview) | [how to rotate text 90 degrees](http://stackoverflow.com/questions/8959069/how-to-rotate-textview-90-degrees-and-display) | [rotated label](http://stackoverflow.com/questions/1258275/vertical-rotated-label-in-android) – sokie Oct 11 '12 at 07:02
  • public class AngledTextView extends TextView { public AngledTextView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { // Save the current matrix canvas.save(); // Rotate this View at its center canvas.rotate(270,this.getWidth()/2, this.getHeight() /2); // Draw it super.onDraw(canvas); // Restore to the previous matrix canvas.restore(); } } i am using this code for textview. – user484848 Oct 11 '12 at 07:04
  • where do you put it? Which kind of layout are you using? – Blackbelt Oct 11 '12 at 07:09
  • i may think your problem lays inside your xml layout – sokie Oct 11 '12 at 07:10
  • i am using linear layout – user484848 Oct 11 '12 at 07:22
  • why don't you put your phone in landscape ? – njzk2 Oct 11 '12 at 07:44
  • It is possible to do this in XML as of API 11 (Android 3.0). http://stackoverflow.com/questions/3774770/sideways-view-with-xml-android – chobok Aug 17 '13 at 06:22

3 Answers3

9

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();
}
}
Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40
1

you should use 270 instead of -90. this will work perfectly.

0

Added in API level 12

    view.animate().rotationBy(90).start()
vmayatskii
  • 144
  • 6