How to Align TextInputLayout Error to the Right in Android

In android programming, we can dynamically align TextInputLayout error to the right in android.

We are going to create a custom TextInputLayout view by extending the default TextInputLayout which will help us to re-position the default error position.

CustomTextInputLayout.java

import android.content.Context;
import android.support.design.widget.TextInputLayout;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;

public class CustomTextInputLayout extends TextInputLayout {

    public CustomTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setErrorEnabled(boolean enabled){
        super.setErrorEnabled(enabled);
        if (!enabled){
            return;
        }
        try{
            Field field = TextInputLayout.class.getDeclaredField("mErrorView");
            field.setAccessible(true);
            TextView errorView = (TextView) field.get(this);
            if(errorView != null){
                errorView.setGravity(Gravity.RIGHT);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.END;
                errorView.setLayoutParams(params);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

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.bluapp.androidview.CustomTextInputLayout
        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.bluapp.androidview.CustomTextInputLayout>


</RelativeLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.bluapp.androidview.CustomTextInputLayout;

public class AlignTextInputLayoutErrorActivity extends AppCompatActivity {

    private CustomTextInputLayout textinputlayout;

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

        textinputlayout = (CustomTextInputLayout) findViewById(R.id.textinputlayout);
        textinputlayout.setErrorEnabled(true);

        textinputlayout.setError("Empty field");
    }
}

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