Skip to content Skip to sidebar Skip to footer

Android Password Toggle Want To Set Text Instead Of Image

I am using TextInputEditText as a password field. I want to use password toggle feature available with support library 24.2.0. passwordToggleDrawable allows me to set drawable to c

Solution 1:

I am achieving this using framelayout. I am showing TextView on top of TextInputEditText

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="allstate.com.testfab.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="16dp"
        app:passwordToggleEnabled="true"
        app:passwordToggleDrawable="@null"
        app:passwordToggleContentDescription="Test1">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:hint="Password"
            android:imeOptions="actionNext"
            android:inputType="textPassword"
            android:maxLength="10"
            android:textCursorDrawable="@null" />
    </android.support.design.widget.TextInputLayout>

    <TextView
        android:id="@+id/toggle_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:text="SHOW"
        android:layout_marginRight="16dp"
        android:paddingTop="10dp"
        />
</FrameLayout>

I am handling onclicks on TextView as below,

textView = (TextView) findViewById(R.id.toggle_button);
        textView.setOnClickListener(newView.OnClickListener() {

            @OverridepublicvoidonClick(View view) {
                visible = !visible;
                if (visible) {
                    textView.setText("HIDE");
                    editText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    textView.setText("SHOW");
                    editText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD);
                }
                editText.setSelection(editable.toString().length());
            }
        });

Please let me know if you find better solution than this.

Post a Comment for "Android Password Toggle Want To Set Text Instead Of Image"