Addy
Addy

Reputation: 85

CardView design issue on lower android versions

I face a strange issue when i use "CardView" to create a button with rounded corners. Let me explain you the design issue i face. Here is the code is use to make round corner button:

<androidx.cardview.widget.CardView
    android:id="@+id/tv_email_next"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_error_message"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="20dp"
    app:cardBackgroundColor="#f15b5d"
    app:cardCornerRadius="34dp"
    app:cardElevation="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/submit_btn_bg">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/next"
            android:textSize="16sp"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:layout_centerVertical="true"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

I use "submit_btn_bg" as backgroudn in RelativeLayout. Here is the code of file "submit_btn_bg":

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f15b5d" />
<corners android:radius="24dp" />
</shape>

Now when i run app in android 7.0 it look like this("Next Button"): Preview in android 7

But when i run same code in Android 10 it look fine like this: Preview in android 10

I not this behavior is in Cardview. Anyone know how i solve this?

Upvotes: 0

Views: 367

Answers (2)

Favour Felix Chinemerem
Favour Felix Chinemerem

Reputation: 1408

This is a better representation of the Button above, the output is very similar to yours and lesser code.

<com.google.android.material.button.MaterialButton
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginEnd="32dp"
    android:backgroundTint="#F44336"
    android:padding="8dp"
    android:text="Next"
    android:textAllCaps="false"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:theme="@style/Theme.MaterialComponents"
    app:cornerRadius="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Output: enter image description here

Upvotes: 1

Branddd
Branddd

Reputation: 98

Its because you should input half of the cardViews height dp in the radius. If your cardview has a height of 50dp, put 25 dp in the submit_btn_bg as a radius.

Also i recommend that you use button as it is much better than using a cardview as button.

Upvotes: 3

Related Questions