How to make TextInputLayout Hint Asterisk Red for Required Fields

In android programming, we can make TextInputLayout hint asterisk red for required fields with the code snippet below.

If you have any other means to achieve this kindly use the comment box below.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textinputlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_centerInParent="true">

        <EditText
            android:id="@+id/fullname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Fullname"
            android:imeOptions="actionUnspecified"
            android:inputType="textNoSuggestions"
            android:maxLines="1"
            android:singleLine="true" />

    </com.google.android.material.textfield.TextInputLayout>

</RelativeLayout>

MainActivity.java

package com.inducesmile.buttonsnippets;

import android.os.Bundle;

import com.google.android.material.textfield.TextInputLayout;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private TextInputLayout textinputlayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textinputlayout = (TextInputLayout) findViewById(R.id.textinputlayout);
        textinputlayout.setHint(textinputlayout.getHint()+" "+getString(R.string.asteriskred));
    }
}

strings.xml

<resources>
    <string name="app_name">EditText Code Snippet</string>
    <string name="asteriskred"><font color='#cc0029'>*</font></string>
</resources>

android text input layout

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment