54

I have this ImageView in my layout:

<ImageView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image_divider"
        android:paddingBottom="8dp"
        android:paddingTop="4dp"
        android:scaleType="fitXY"
        android:src="@android:drawable/divider_horizontal_textfield" />

It's a horizontal divider. I want to rotate it 90 degrees so I have a vertical divider.
Is there any possible way to do it right here from the layout and not the Activity class?

mehrmoudi
  • 1,096
  • 1
  • 11
  • 22
  • you can just set the height to `layout_height="fill_parent"` and then it'll be stretched all over the layout – thepoosh Jun 28 '12 at 11:09
  • Kindly accept/upVote the answer if you have got your solution :) – RPB Jun 28 '12 at 11:13
  • @thepoosh: The source image is not a square. It's a wide rectangle and doing what you say, will result in a thin vertical rectangle. – mehrmoudi Jun 28 '12 at 11:20

3 Answers3

177

You can use Available Since API Level 11

android:rotation="90"

Final Code to Put,

<ImageView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:rotation="90"
        android:contentDescription="@string/image_divider"
        android:paddingBottom="8dp"
        android:paddingTop="4dp"
        android:scaleType="fitXY"
        android:src="@android:drawable/divider_horizontal_textfield" />
RPB
  • 16,006
  • 16
  • 55
  • 79
  • 1
    Thanks, but there is no such attribute for ImageView, at least in mine! http://s15.postimage.org/gpit1dvrd/Untitled.png – mehrmoudi Jun 28 '12 at 11:18
  • @Mehran Visit http://developer.android.com/reference/android/view/View.html#attr_android:rotation For More Information – RPB Jun 28 '12 at 11:20
  • 1
    I disagree: android:roation attribute is only available since API 11: http://developer.android.com/reference/android/R.attr.html#rotation – Jeje Doudou Jun 28 '12 at 11:20
  • @Jeje You can visit http://developer.android.com/reference/android/view/View.html#attr_android:rotation For more details link which you have shared is incorrect in this context. :) – RPB Jun 28 '12 at 11:22
  • Sure, but then, follow the link: "This corresponds to the global attribute resource symbol rotation." – Jeje Doudou Jun 28 '12 at 11:24
  • @MehranMahmoudi Check for version you are developing on, See my updated answer – RPB Jun 28 '12 at 11:27
  • @Rinkalkumar I changed the target API from 10 to 15 in my project configurations, and android:rotation showed up. So there is no any other possible way for API 10? – mehrmoudi Jun 28 '12 at 11:27
  • 2
    @MehranMahmoudi There is no way you can rotate programmatically in API 10, One thing you can do is you can have two image one with 90 Degree rotation and one is default, :) – RPB Jun 28 '12 at 11:29
  • @Pankaj It starts working from API Level 11 whats the device and OS you are using? – RPB Mar 04 '15 at 08:34
  • 4
    thanks,Now it works. I was expecting rotation would reflect in xml, but after running my project it gets rotated!! – Pankaj Mar 04 '15 at 08:37
  • Please note that the rotation is in Clockwise. – ImMathan Aug 31 '15 at 12:36
  • how can I rotate with one end being fixed? – arqam Feb 20 '16 at 06:29
3

Add "id" at ImageView (if not generate auto):

 android:id="@+id/imageView"

and use the "id" (kotlin example):

val imageView = findViewById<ImageView>(R.id.imageView)
imageView.setRotation(90f) // rotate 90 degree
1

You can do that in your code by creating a new bitmap object. Check this out : http://android-er.blogspot.fr/2010/07/rotate-bitmap-image-using-matrix.html And specifically this function

Matrix matrix = new Matrix();
matrix.postScale(curScale, curScale);
matrix.postRotate(curRotate);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
myImageView.setImageBitmap(resizedBitmap);
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69