Is it possible to show the word inserted in a password field by clicking on a button and then when I click it again hide the password as dots?
I'll really appreciate your help.
Is it possible to show the word inserted in a password field by clicking on a button and then when I click it again hide the password as dots?
I'll really appreciate your help.
Try this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="100sp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200sp"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/showHideBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"/>
</LinearLayout>
</LinearLayout>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showHideBtn.setOnClickListener {
if(showHideBtn.text.toString().equals("Show")){
pwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
showHideBtn.text = "Hide"
} else{
pwd.transformationMethod = PasswordTransformationMethod.getInstance()
showHideBtn.text = "Show"
}
}
}
}