How to rotate TextView not just its text

Viewed 1279

I need to rotate my text view, so I just used this code:

<TextView
                        android:rotation="90"
                        android:layout_gravity="center"
                        android:id="@+id/price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/stock_price"
                        android:textAppearance="@style/font"
                        android:textColor="@color/gray" />

but the problem is just my text will rotate not text view. this is shown in the image below:

enter image description here

How can I rotate the whole text view not just its text? ( as you can see in the image, the text "my rotated text" is rotated for 90 degrees but textView, itself, is still in a horizontal line.

I also tried fixing that using animations. so I tried this code, but nothing changed:

 binding.price.animate().rotation(90F).start()

any help will be appreciated.

1 Answers

You can Group both of them inside ConstraintLayout and rotate the the whole ViewGroup something like that :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotation="90"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginTop="8dp"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintEnd_toEndOf="@+id/tex"
            app:layout_constraintStart_toStartOf="@+id/tex"
            app:layout_constraintTop_toBottomOf="@+id/tex" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

changing orientation gives you that :

enter image description here

enter image description here

Update

if you want to rotate the TextView only you will need two steps

1-> Rotate the whole layout (Containing Both) say "90" degree

2-> Return the ImageView back to it's position by rotating it with "-90" degree

Related