Rotating a view in Android

AndroidViewRotation

Android Problem Overview


I have a button that I want to put on a 45 degree angle. For some reason I can't get this to work.

Can someone please provide the code to accomplish this?

Android Solutions


Solution 1 - Android

API 11 added a setRotation() method to all views.

Solution 2 - Android

You could create an animation and apply it to your button view. For example:

    // Locate view
    ImageView diskView = (ImageView) findViewById(R.id.imageView3);
   
    // Create an animation instance
    Animation an = new RotateAnimation(0.0f, 360.0f, pivotX, pivotY);
   
    // Set the animation's parameters
    an.setDuration(10000);               // duration in ms
    an.setRepeatCount(0);                // -1 = infinite repeated
    an.setRepeatMode(Animation.REVERSE); // reverses each repeat
    an.setFillAfter(true);               // keep rotation after animation
   
    // Aply animation to image view
    diskView.setAnimation(an);

Solution 3 - Android

Extend the TextView class and override the onDraw() method. Make sure the parent view is large enough to handle the rotated button without clipping it.

@Override
protected void onDraw(Canvas canvas) {
     canvas.save();
     canvas.rotate(45,<appropriate x pivot value>,<appropriate y pivot value>);
     super.onDraw(canvas);
     canvas.restore();

} 

Solution 4 - Android

I just used the simple line in my code and it works :

myCusstomView.setRotation(45);

Hope it works for you.

Solution 5 - Android

One line in XML


<View
    android:rotation="45"
    ... />

Solution 6 - Android

Applying a rotation animation (without duration, thus no animation effect) is a simpler solution than either calling View.setRotation() or override View.onDraw method.

// substitude deltaDegrees for whatever you want
RotateAnimation rotate = new RotateAnimation(0f, deltaDegrees, 
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  

// prevents View from restoring to original direction. 
rotate.setFillAfter(true); 

someButton.startAnimation(rotate);

Solution 7 - Android

Rotating view with rotate() will not affect your view's measured size. As result, rotated view be clipped or not fit into the parent layout. This library fixes it though:

https://github.com/rongi/rotate-layout

enter image description here

Solution 8 - Android

Joininig @Rudi's and @Pete's answers. I have created an RotateAnimation that keeps buttons functionality also after rotation.

setRotation() method preserves buttons functionality.

Code Sample:

Animation an = new RotateAnimation(0.0f, 180.0f, mainLayout.getWidth()/2, mainLayout.getHeight()/2);

	an.setDuration(1000);              
	an.setRepeatCount(0);                	  
	an.setFillAfter(false);              // DO NOT keep rotation after animation
	an.setFillEnabled(true);             // Make smooth ending of Animation
	an.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            	mainLayout.setRotation(180.0f);      // Make instant rotation when Animation is finished
            }
	    	}); 

mainLayout.startAnimation(an);

mainLayout is a (LinearLayout) field

Solution 9 - Android

As mentioned before, the easiest way it to use rotation available since API 11:

android:rotation="90"    // in XML layout

view.rotation = 90f      // programatically

You can also change pivot of rotation, which is by default set to center of the view. This needs to be changed programatically:

// top left
view.pivotX = 0f
view.pivotY = 0f

// bottom right
view.pivotX = width.toFloat()
view.pivotY = height.toFloat()

...

In Activity's onCreate() or Fragment's onCreateView(...) width and height are equal to 0, because the view wasn't measured yet. You can access it simply by using doOnPreDraw extension from Android KTX, i.e.:

view.apply {
    doOnPreDraw {
        pivotX = width.toFloat()
        pivotY = height.toFloat()
    }
}

Solution 10 - Android

if you wish to make it dynamically with an animation:

view.animate()
    .rotation(180)
    .start();

THATS IT

Solution 11 - Android

@Ichorus's answer is correct for views, but if you want to draw rotated rectangles or text, you can do the following in your onDraw (or onDispatchDraw) callback for your view:

(note that theta is the angle from the x axis of the desired rotation, pivot is the Point that represents the point around which we want the rectangle to rotate, and horizontalRect is the rect's position "before" it was rotated)

canvas.save();
canvas.rotate(theta, pivot.x, pivot.y);
canvas.drawRect(horizontalRect, paint);
canvas.restore();

Solution 12 - Android

fun rotateArrow(view: View): Boolean {
    return if (view.rotation == 0F) {
        view.animate().setDuration(200).rotation(180F)
        true
    } else {
         view.animate().setDuration(200).rotation(0F)
         false
    }
}

Solution 13 - Android

That's simple, in Java

your_component.setRotation(15);

or

your_component.setRotation(295.18f);

in XML

<Button android:rotation="15" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthewView Question on Stackoverflow
Solution 1 - AndroidJens ZalzalaView Answer on Stackoverflow
Solution 2 - AndroidPeteView Answer on Stackoverflow
Solution 3 - AndroidIchorusView Answer on Stackoverflow
Solution 4 - AndroidRudiView Answer on Stackoverflow
Solution 5 - AndroidMichaelView Answer on Stackoverflow
Solution 6 - AndroidDYSView Answer on Stackoverflow
Solution 7 - AndroidDmitry RyadnenkoView Answer on Stackoverflow
Solution 8 - AndroidMichael SView Answer on Stackoverflow
Solution 9 - AndroidMicerView Answer on Stackoverflow
Solution 10 - AndroidIvan YulinView Answer on Stackoverflow
Solution 11 - Androidgauravjain0102View Answer on Stackoverflow
Solution 12 - AndroidJob MView Answer on Stackoverflow
Solution 13 - AndroidAffa MusaffaView Answer on Stackoverflow