Search code examples
androidandroid-custom-drawable

Stroke with text


I was wondering that is it possible to create a custom drawable with stroke as border and this stroke contains text and for my ths question see the following image that I have created on Paint. enter image description here

I have created a Linear Layout with stroke and a text 'Hello World' is on the stroke, so the question once again is that is it possible to create this type of stroke.


Solution

  • Your expected design can be achieved by the following code I just did and checked:

    First use a custom background in drawable: bg_gray_rectangle_rounded_corners.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="rectangle" 
         xmlns:android="http://schemas.android.com/apk/res/android">
    
        <stroke android:width="1.5dp"
            android:color="@color/colorPrimary"/>
    
        <corners android:radius="8dp"/>
    
        <solid android:color="#00B6625D"/>
    
    </shape>
    

    then in your main xml file copy and paste this snippet:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
    android:layout_gravity="center"
    android:background="#FFF"
    android:orientation="vertical">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="15dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@drawable/bg_gray_rectangle_rounded_corners"
            android:gravity="center"
            android:orientation="horizontal"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            android:weightSum="2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/bg_gray_rectangle_rounded_corners"
                android:gravity="center"
                android:padding="10dp"
                android:text="Log In"
                android:textColor="@color/colorPrimary" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/bg_gray_rectangle_rounded_corners"
                android:gravity="center"
                android:padding="10dp"
                android:text="Sign up"
                android:textColor="@color/colorPrimary" />
        </LinearLayout>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginLeft="32dp"
            android:layout_marginTop="7dp"
            android:background="#FFF"
            android:padding="4dp"
            android:text="@string/app_name"
            android:textColor="#636262"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    </LinearLayout>
    

    Here is the output

    output

    Have a good day. Let me know if it helps.