I'd like to use AlertDialog as a Login or pin code or password dialog. Here is my code -
AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Login"); alert.setMessage("Enter Pin :"); // Set an EditText view to get user input final EditText input = new EditText(this); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString(); Log.d( TAG, "Pin Value : " + value); return; } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub return; } }); alert.show();
How to code that all input text will appear like ' *** ' - asterisk
I can't get my pin code value although it shows into asterisk. my code is below
private void accessPinCode_2() { LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.dialog_login, null); AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Login"); alert.setMessage("Enter Pin :"); alert.setView(textEntryView); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //String value = input.getText().toString(); EditText mUserText; mUserText = (EditText) textEntryView.findViewById(R.id.txt_password); String strPinCode = mUserText.getText().toString(); Log.d( TAG, "Pin Value : " + strPinCode); return; } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub return; } }); alert.show(); }}
dialog_login.xml
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/txt_password" android:password="true" android:layout_height="wrap_content" android:layout_width="250px" android:layout_centerHorizontal="true" android:layout_below="@+id/password_text" android:singleLine="true" />
I entered the value, but get null!
how to solve?
Debugging is stopped at this statement. When I pointed above mUserText null value being shown in popup.
String strPinCode = mUserText.getText().toString();
I use Android 1.6. Does it depend on Version? :?:
A simple dialog containing an DatePicker . This class was deprecated in API level 26.
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog. A dialog with a pre-defined UI that allows the user to select a date or time.
If you only want to change text format, you can just override alertDialogTheme attribute to change the theme for the AlertDialog . <style name="MyTheme" parent="Theme. AppCompat.
This problem can be solved without using deprecated android:password
. See my answer here.
You EditText
should have android:password="true"
.
Check this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With