I would like to create a component in Android. The component should be a border with title similar to the TextInputLayout with the possibility to include any layout inside.
I have found one solution in here, but I would like to know if someone could share a different solution. I think that one is hacky.
The layout includes two linear layouts, the first one is the top border line with the title, and the second is the content with a left-, right- and bottom-border.
What do you think?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:weightSum="2">
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_weight="1"
android:background="#2ebadc"/>
<TextView
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Hello World" />
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginRight="5dp"
android:background="#2ebadc"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:padding="10dp"
android:background="@drawable/topless_border">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is a border with title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="The top inset of the border is set to -3 to make it disappear and then it's covered with a line with text in the middle by using LinearLayout with two Views and one TextView"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-3dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke android:width="2dp" android:color="#2ebadc" />
<corners android:radius="1dp" />
</shape>
</item>
</layer-list>
Thanks in advance.

