By Matthew

2009-12-18 22:09:46 8 Comments

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?


@Dmitry Ryadnenko 2014-03-26 11:23:10

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:

enter image description here

@Alp Altunel 2017-08-17 11:36:33

this is nice work but if the control is wider than screen size? how to make it fit to screen?

@Michael 2015-09-14 17:36:37

One line in XML

    ... />

@DYS 2014-02-26 06:03:48

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. 


@Ichorus 2009-12-19 03:15:42

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.

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


@Matthew 2009-12-20 16:39:07

Ok that sorta accomplish's what i want. I want the entire view itself to be on a 45 degree angle. Just not the text inside. But thank you for answering my question

@Alexey 2012-07-10 13:33:23

If you want to rotate the entire view (along with its background), you should oveerride draw() instead of onDraw().

@Kailash Dabhi 2013-11-19 08:46:51

ok it works if using draw()method rotate the whole button but not drawing view correctly

@Denis Gladkiy 2016-03-07 06:20:31

There is no need to save whole canvas state, is enough.

@Jens Zalzala 2012-03-08 23:36:59

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

@Jens Zalzala 2017-12-05 18:58:33

@GerhardBarnard setRotation() is the method to use and is the answer itself. The link is only there for convenience.

@Michael S 2014-01-04 16:16:54

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.setFillAfter(false);              // DO NOT keep rotation after animation
    an.setFillEnabled(true);             // Make smooth ending of Animation
    an.setAnimationListener(new AnimationListener() {
        public void onAnimationStart(Animation animation) {}

        public void onAnimationRepeat(Animation animation) {}

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


mainLayout is a (LinearLayout) field

@Rudi 2013-11-22 06:55:47

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


Hope it works for you.

@nicopico 2014-02-25 14:13:06

Note: this method is available since API 11 (Honeycomb)

@user512 2015-04-02 04:17:12

after rotating the view(ImageView) is it possible to save the image with that rotation??

@Tuss 2015-11-13 03:57:33

simple and nice.

@gauravjain0102 2011-12-26 11:03:28

@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.rotate(theta, pivot.x, pivot.y);
canvas.drawRect(horizontalRect, paint);

@Pete 2011-09-28 22:04:01

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

    // Locate view
    ImageView diskView = (ImageView) findViewById(;

    // 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

@Andras Balázs Lajtha 2012-05-22 06:02:59

Nice to see that things that can't be done defining layouts can be achieved with animations and setFillAfter(true).

@Warpzit 2012-06-04 13:13:33

This will rotate the view all right, but buttons wont have their functionality rotated. Not the holy grail for me :)

@Warpzit 2012-12-12 10:05:57

It turns out that there isn't any way to rotate functionality of a button. I ended up forwarding events to the buttons based on where the screen was touched.

Related Questions

Sponsored Content

97 Answered Questions

[SOLVED] Close/hide the Android Soft Keyboard

31 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

24 Answered Questions

48 Answered Questions

[SOLVED] Is there a unique Android device ID?

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

43 Answered Questions

31 Answered Questions

[SOLVED] Activity restart on rotation Android

77 Answered Questions

10 Answered Questions

[SOLVED] Hex transparency in colors

Sponsored Content