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?

3
  • you can just set the height to layout_height="fill_parent" and then it'll be stretched all over the layout
    – thepoosh
    Jun 28, 2012 at 11:09
  • Kindly accept/upVote the answer if you have got your solution :)
    – RPB
    Jun 28, 2012 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, 2012 at 11:20

3 Answers 3

178

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" />
12
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.