android – Drawable view background with gradient showing correctly in preview but not in emulator-ThrowExceptions

Exception or error:

I want to create a divider like view which fades to the edges. Therfore I’ve created a drawable shape with a gradient to both sides which I am setting as Background of a View. In my XML preview my desired result is shown but when I launch the App in the emulator the drawable is rotated 90 degreese and I dont know why that is.

This is my shape

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:centerColor="@color/white"
        android:endColor="@color/transparent"
        android:startColor="@color/transparent"
        android:type="linear" />
</shape>

this is my view

<View
     android:layout_width="256dp"
     android:layout_height="100dp"
     android:background="@drawable/horizontal_fading_divider"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintTop_toTopOf="@id/recycler_view"
     android:layout_marginTop="40dp"/>

This is how it should look like (Preview)

This is how it actually looks

How to solve:

I found an answer thanks to Nice umang.

You have to use the angle property even thought you dont want to rotate your shape to be safe it wont be rotated elsewhere.

Like so

android:angle="0"

Leave a Reply

Your email address will not be published. Required fields are marked *