Search code examples
javaandroidandroid-custom-view

Rounded view with title and text inside


How can I make this? It looks nice and I'd like to use something like this.

Picure

The lines at the top should correspond to the end of the title


Solution

  • Actually, these types of view are mostly done with custom views.

    This tutorial is useful for custom views.

    But you can cheat a little bit, create backgound.xml inside the drawable folder, and paste the code:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
    
        <stroke android:color="@color/blue" android:width="4dp"/>
        <corners android:radius="10dp"/>
        <solid android:color="@color/white"/>
    </shape>
    

    then create custom_background.xml for your layout, and paste the following code

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            xmlns:app="http://schemas.android.com/apk/res-auto"
        >
        <LinearLayout
    
                android:background="@drawable/background"
    
                android:layout_marginTop="15dp"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <TextView
                    android:textSize="20sp"
                    android:textAlignment="center"
                    android:textColor="@color/blue"
                    android:layout_gravity="center"
                    android:text="Something"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
        </LinearLayout>
        <TextView
                android:padding="2dp"
                android:textColor="@color/blue"
                android:textSize="25sp"
                android:background="@color/white"
                android:layout_marginStart="50dp"
                android:text="Title"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
        />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Then you will get the following picture

    enter image description here

    Note

    • I am using androidX
    • Instead of LinearLayout you can place any layout