[Android] Implement a function to display passwords quickly

This is a different method from Password Visibility Toggle. Sampler is here

image

Implementation

Pattern0 is a guy, but this time I will introduce Pattern1. I'm using TextInputLayout for clarity, but it's okay not to use it I want to customize without relying on Password Visibility Toggle! It is for people.

XML

activity_main.xml


<!--Abbreviation-->
    <!-- Pattern 1 -->
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- password -->
        <android.support.design.widget.TextInputEditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password"/>
    </android.support.design.widget.TextInputLayout>

    <!--Show / hide password-->
    <CheckBox
        android:id="@+id/password_visibility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:text="Show password"/>

<!--Abbreviation-->

Activity

MainActivity.java



//Abbreviation

public class MainActivity extends AppCompatActivity {

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

        TextInputEditText password = (TextInputEditText)findViewById(R.id.password);

        CheckBox passwordVisibilityCheckBox = (CheckBox) findViewById(R.id.password_visibility);

        //Password switching checkbox
        passwordVisibilityCheckBox.setOnClickListener((View v) -> {

            //Save password cursor position
            int cursorPos = password.getSelectionStart();

            boolean checked = passwordVisibilityCheckBox.isChecked();
            if (checked) {
                //Password visualization
                password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                //Cursor position set
                password.setSelection(cursorPos);
            } else {
                //Password invisibility
                password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                password.setSelection(cursorPos);
            }
        });
    }
}


Yes completed!

At the end

There is a better implementation! I'm waiting for your comment! !!

Recommended Posts

[Android] Implement a function to display passwords quickly
Android Development-Try to display a dialog-
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
I want to implement a product information editing function ~ part1 ~
How to implement one-line display of TextView in Android development
Try to implement iOS14 Widget function
[Rails] A simple way to implement a self-introduction function in your profile
[Behavior confirmed in December 2020] How to implement the alert display function
Sample to display (head-up) notifications on Android
Try to implement login function with Spring-Boot
[Swift] How to implement the countdown function
Android Easily give a "press" to a button
How to implement TextInputLayout with validation function
Ability to display a list of products
"Teacher, I want to implement a login function in Spring" ① Hello World
A note about adding Junit 4 to Android Studio
Introduction to Recursive Functions: What is a Recursive Function?
[Android] How to convert a character string to resourceId
How to display a web page in Java
Add a tag function to Rails. Use acts-as-taggable-on
[swift5] How to implement the Twitter share function
How to implement the breadcrumb function using gretel
Try to implement login function with Spring Boot
[For beginners] How to implement the delete function
Flow to implement image posting function using ActiveStorage
(Android) Try to display static text using DataBinding
How to implement a like feature in Rails
[Android] Two ways to get a Bluetooth Adapter
[Swift] How to implement the fade-in / out function
[Java] 4 steps to implement splash screen on Android
How to make a follow function in Rails
I tried to implement a server using Netty
Let's implement a function to limit the number of access to the API with SpringBoot + Redis
How to implement a like feature in Ajax in Rails
[Rails, JS] How to implement asynchronous display of comments
I tried to make a login function in Java
How to display a browser preview in VS Code
Quickly implement a singleton with an enum in Java
I want to define a function in Rails Console
Rails learning How to implement search function using ActiveModel
[Introduction to Android application development] Let's make a counter
Try to implement tagging function using rails and js
I want to add a delete function to the comment function
I tried to make a message function of Rails Tutorial extension (Part 2): Create a screen to display