Free Education

Online Education

Android Studio

android edittext show hide password

hello android developer in this tutorial, you’ll learn how to use edittext activity in android studio.EditText is used to get input from the user

XML (activity_main.xml)

<android.support.constraint.ConstraintLayout
    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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Enter Password"
        android:textAppearance="?android:attr/textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@+id/lblPassword"
        tools:layout_editor_absoluteX="0dp">

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password" />

</android.support.constraint.ConstraintLayout>

JAVA (MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Password extends AppCompatActivity {
    private EditText password;
    private Button btn;

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

        addListenerOnButton();
    }
    public void addListenerOnButton() {
        password = (EditText) findViewById(R.id.password);
        btn = (Button) findViewById(R.id.btn5);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(Password.this, password.getText(),
                        Toast.LENGTH_SHORT).show();

            }

        });

    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *