9

Im trying to rotate a textview but when i rotate it, it keeps the width and height information

i want to the selected area gone i just want to text appears on the green indicator aligned centered. i'm changing the text 'Small Text' in java part but when the length of the text changes the alignment goes crazy (picture3)

here is my code what am i doing wrong ?

<ImageView
        android:id="@+id/statusIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/bulletgreen"
        android:layout_marginRight="5dp"
         />

        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:rotation="-90"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="10sp"
            android:textStyle="bold" />

http://postimg.org/image/9e92i8k2j/ -> 2 http://postimg.org/image/lfdj7udhv/ -> 3

cgtyklnc
  • 93
  • 1
  • 4
  • i'm using geny motion emulator which runs 4.1.1 version but i'm actually tryin on real device too but when i'm rotating the text as you see in screenshots it keeps the width and shows the text out of the boundries of the object – cgtyklnc Apr 26 '14 at 16:24

3 Answers3

14

You're probably best served by writing a simple custom control that inherits from TextView (via nidhi)

Here's a copy of the class.

public class VerticalTextView extends TextView {
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 void onDraw(Canvas canvas) {
    if (topDown) {
        canvas.translate(getWidth(), 0);
        canvas.rotate(90);
    } else {
        canvas.translate(0, getHeight());
        canvas.rotate(-90);
    }

    canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
    getLayout().draw(canvas);
}
}

There are some drawbacks to this solution (mentioned in the blog post), but it works pretty well. This is the simplest and most isolated solution I've found. It can be dropped into a code file and immediately put to work. Plus, it shows up correctly in the layout preview, unlike most hacks involving rotation animations.

Community
  • 1
  • 1
Cody Toombs
  • 4,380
  • 2
  • 15
  • 18
  • 1
    Thanks man, works like butter. After brainstorming for 6-7 hours found this help for making perfect `VerticalTextView`. Thanks again. Although it have some extra things which are not needed, made an edit if you wan't you can accept. – Ankit Bansal Jul 10 '15 at 17:25
  • 5
    great solution , the problem is now after rotation I can't change my TextView color anymore and it's always in black – Rudi Jan 01 '17 at 17:10
  • Any suggestions regarding how to set fontfamily, textcolor? – Amrish Kakadiya Sep 20 '21 at 05:42
  • To keep the textColor like set in your XML you should add `super.onDraw(canvas)` as the last line of the onDraw method – Mr. Robot Oct 01 '21 at 13:12
1

I used a navigate margin to remove the extra space.

<TextView
    android:rotation="-90"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="-10dp"
    android:layout_marginRight="-10dp"/>
JOE
  • 353
  • 2
  • 11
1

Put the TextView in a FrameLayout

    <FrameLayout
      android:id="@+id/frameLayoutMarqueeText"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

        <TextView
          android:id="@+id/textViewMarquee"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="the difference between tomorrow and yesterday is that tomorrow is the day after the present day while yesterday is the day before today it can also be understood as a day ago." />
    </FrameLayout>

And then set the width, height of FrameLayout to the height, width of the parent or screen. (Interchange each other).

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
frameLayoutMarqueeText.setLayoutParams(new LinearLayout.LayoutParams(height, width));
imok1948
  • 81
  • 1
  • 3