Search code examples
androidkotlinandroid-drawableshapes

To draw rounded rectangle in Android


I have found this question and the solution was this code :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#ffffff" />

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

This code doesn't work on my PC (no rounded corners). And you ? There has been changes ?

EDIT 1: I have an error when I build the project : AAPT: error: XML or text declaration not at start of entity. (finally corrected : stupid mistake from me)

EDIT 2 : The error is now : The following classes could not be found: - corners (Fix Build Path, Edit XML) - shape (Fix Build Path, Edit XML) - solid (Fix Build Path, Edit XML) Tip: Try to build the project.

XML layout :

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <shape
        android:shape="rectangle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <solid android:color="#ffffff" />

        <corners
            android:bottomLeftRadius="120dp"
            android:bottomRightRadius="120dp"
            android:topLeftRadius="120dp"
            android:topRightRadius="120dp" />

    </shape>
</androidx.constraintlayout.widget.ConstraintLayout>

SOLUTION :

here


Solution

  • I think you couldn't see the rounded rectangle as the radii are small so that they might not be noticed, try to set larger values for the four corners

    Here is 120dp radii

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid android:color="#ffffff" />
    
        <corners
            android:bottomLeftRadius="120dp"
            android:bottomRightRadius="120dp"
            android:topLeftRadius="120dp"
            android:topRightRadius="120dp" />
    
    </shape>
    

    enter image description here

    UPDATE

    EDIT 2 : The error is now : The following classes could not be found: - corners (Fix Build Path, Edit XML) - shape (Fix Build Path, Edit XML) - solid (Fix Build Path, Edit XML) Tip: Try to build the project.

    You can't use drawable tags into xml layout directly like <shape> or layer-list, instead you can refer to drawable resource with some layout view attributes like android:background as below

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/test"
            android:padding="8dp"
            android:text="Hello World!"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="22sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    enter image description here