Custom EditText field with „required“ attribute

To do that you need to create new extended Class, for example „CustomField“:

package my.package;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatEditText;

public class CustomField  extends AppCompatEditText {
    private boolean required = false;
    public CustomField(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.SettingsField,
                0, 0);
        try {
            required = array.getBoolean(R.styleable.CustomField_required, false);
        } finally {
            array.recycle();
        }
    }
    public boolean isRequired() {
        return required;
    }
}

Inside of „values“ folder add following attributes to attr.xml file (create new, if is not exists):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomField">
        <attr name="required" format="boolean" />
    </declare-styleable>
</resources>

Now you can create xml layout:

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/wrapper"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@android:color/white">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:labelFor="@+id/field"
            android:text="@string/label_field"
            android:background="@android:color/white"/>
        <my.package.CustomField
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:required="true"
            android:layout_margin="1dp"
            android:hint="@string/hint_field"
            android:text=""
            android:id="@+id/field"
            android:background="@android:drawable/editbox_background_normal"/>
    </LinearLayout>
</LinearLayout>