Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sideways View with XML Android

Is there a way to display a View, such as a TextView sideways using the XML layout file? I know you can rotate the view using code in an activity, but is there a way just to do it with layout?

like image 755
ramblinjan Avatar asked Sep 23 '10 01:09

ramblinjan


2 Answers

There is, but it's new in API 11 (Android 3.0):

<TextView
    android:id="@+id/rotated"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:rotation="270"
    android:textSize="32sp"
    android:textColor="#44CC44"
    android:text="@string/rotated" />

I tried this in 2.2, 2.3.1, and 2.3.3, and "android:rotation" wasn't legal.

It worked in 3.0 on the emulator, but it was odd. I also added paddingTop="90dp" to it (to get it away from another component in the test app I tried it in), and it not only moved down the screen, but also to the right (away from the top of the text, since it's rotated 270 degrees). I don't have an actual Honeycomb device to test on, but it looks like rotation and padding/margins might not play well together.

There is also an android.view.View#setRotation(double) method that has been added in API 11 which tweaks this property from code. And in addition to rotation, there is rotationX, rotationY, pivotX, pivotY, scaleX, scaleY, and translationX, translationY. I haven't played with any of them yet.

There is a list of all the changes in API 11.

But I suspect you wanted something that is not only limited to Honeycomb. As far as I know, there isn't.

like image 167
David Conrad Avatar answered Nov 15 '22 18:11

David Conrad


David is right, the :rotation WILL work but something is messed up in the SDK I believe. The Graphical Layout will cause the text to move far left or right (and possible off screen) so it BUT when I run it on my Xoom, it works as expected. So try the settings on an API 11 device to see it's affect.

Also, It looks as if it does something with the width/height as well. The only way I can get it to work is to make the text view square. In other words, if you normally only needed a 100dp wide by 25dp high textview, make it 100dp x 100dp then rotate and try it. Also, top/bottom becomes left/right, etc when you rotate 90 degrees.

I can't figure out what RotationX/Y does. It seems to do nothing for me.

like image 29
Jamz Avatar answered Nov 15 '22 18:11

Jamz