Search code examples
androidandroid-edittextandroid-textinputlayoutandroid-textinputedittext

EditText change setError text position


So so what im trying to implement has two parts

One im giving my edittext the passwordToggle for which im using android.support.design.widget TextInputLayout+TextInputEditText

So this is how my edittext looks like

edittext with passwordToggle

Part two is i want to add validation and set appropriate error message. I need the error message to be shown as follows

edittext with custom setError

My layout code is as follows

<android.support.design.widget.TextInputLayout
    style="@style/editTextBold"
    android:id="@+id/input_pwd_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintAnimationEnabled="false"
    app:hintEnabled="false"
    app:passwordToggleEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/input_pwd"
        style="@style/editTextBold"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:ems="10"
        android:hint="@string/hint_pwd"
        android:inputType="textPassword"
        android:padding="10dp" />
</android.support.design.widget.TextInputLayout> 

So what i want to know is

1.How do i hide/unhide the password toggle icon in the edittext via code?

2.Also how do i make the setError message appear in place of the passwordToggle icon(once i hide it via code)


Solution

  • Ended up creating a custom view

    enter image description here

    The component can be found here

    https://github.com/vnh1991/CustomValidatorEditText

    Import the lib project as a module ,

    in your layout create a container

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.v2dev.customedittextdemo.MainActivity">
    
        <LinearLayout
            android:id="@+id/llContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical"></LinearLayout>
    
        <Button
            android:id="@+id/btnValidate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_below="@+id/llContainer"
            android:padding="10dp"
            android:text="Validate" />
    
    </RelativeLayout>
    

    And in your activity load the component into the container

    package com.v2dev.customedittextdemo;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    
    import com.v_sQr_dev.customvalidatoredittext.CustomEdittext;
    
    public class MainActivity extends AppCompatActivity {
    
        private LinearLayout llContainer;
        private Button btnValidate;
        private CustomEdittext inputPwd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            llContainer = (LinearLayout) findViewById(R.id.llContainer);
            btnValidate = (Button) findViewById(R.id.btnValidate);
            inputPwd = new CustomEdittext(llContainer, this);
            inputPwd.setHint("PASSWORD");
    
            btnValidate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(inputPwd.validate()){
                        Toast.makeText(MainActivity.this,"Input Valid",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }