0

I am working on a project for the university using Android.

I have a layout -XML- with a simple button, I would like to rotate it so that it is always in the same position, independently if the phone is in portrait mode or in horizontal mode

I'm just trying to rotate a button, I do not want any animation.

Here is my sample XML.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:layoutDirection="ltr"
android:padding="5dp">


<Button
    android:id="@+id/punto3"
    android:layout_width="185dp"
    android:layout_height="68dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"

    android:layout_marginStart="0dp"
    android:layout_marginTop="177dp"
    android:layout_weight="1"
    android:background="@color/colorPrimary"


    android:rotation="90"


    android:text="3" />


</RelativeLayout>

FIRSTLY: I added: android:rotation="90" to rotate from horizontal to vertical my rectangle -button-.

Secondly: I check Design page, I got this: [https://i.stack.imgur.com/CPWyW.png

Question How can I rotate my button without animation or calculations? Just a simple turn. Something easy. Because I checked this questions: Android: Rotate whole layout.

Thank you for your time.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
James Garcia
  • 93
  • 2
  • 5
  • Does rotation in xml works for you? Cus if it works you can try punto3.setRotation(angle). – Elvedin Selimoski Dec 19 '18 at 09:13
  • It works, but when I want to move my button to the left margin - in contact with the edge of the left screen - I can not. the button thinks it is still in horizontal mode. – James Garcia Dec 19 '18 at 09:27
  • Replace RelativeLayout with ConstraintLayout if setRotation works. set `layout_constraintLeft_toLeftOf=parent` in portrait mode – Payam Asefi Dec 19 '18 at 09:35
  • rotation is little tricky when you come to View's as per ur need u have to set the pivotX and pivotY around which u want to rotate and then rotate the view.In your Screenshot view's Pivot are at centre of the view change pivotX and pivotY to (0,0) @JaimeGar – Anmol Dec 19 '18 at 09:41
  • Thank you all. I will try to force open in Landscape. Everything you all said work, but if i rotate all my LinearLayout (android:layout_weight="1") does not work correctly. – James Garcia Dec 19 '18 at 10:17

0 Answers0