views:

599

answers:

3

Hi,

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"
        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: 

You EditText should have android:password="true".

Check this link.

Macarse
Thank you, Macarse. I know this android:password. But I just want to use AlertDialog as Login form. Is it good to use like that? My application has main tab layout and then related activity will appear. It looks like built-in contacts. I'd like to appear this login layout whenever user starts to use my application. I just know onResume() event but if i use this event, sometimes login layout will appear twice or more. So how to code to appear each time user starts to use application.
It worked. I use LayoutInflater, based on this link - http://www.coderanch.com/t/434894/Android/Mobile/values
I can't get my pin code value although it shows into asterisk. I posted my updated code in my post. Please check my missing or wrong point.
Yap! I get it! my fault is in xml. I missed to type "android" in "id" declaration part. :P =D
A: 

This problem can be solved without using deprecated android:password. See my answer here.

Viktor Bresan
A: 

can any one tell me how to read value from the EditText of the alertdialog..... in the following code it throws a nullpointer exception

View.OnClickListener clickable=new View.OnClickListener() 
    {

        @Override


        ////------ClickListener definition for calculating and displaying 
    try {
    if(v==(Button)findViewById(R.id.widget31))
    {
        double r=0.0;

        AlertDialog ad = new AlertDialog.Builder(this).create();
        LayoutInflater inflater = LayoutInflater.from(this);
        AbsoluteLayout al=new AbsoluteLayout(this);
        View wind=inflater.inflate(R.layout.dialogbox,al);
        ad.setView(wind);                       
        EditText rate=(EditText)findViewById(R.id.rate);
        Button calculate=(Button)findViewById(R.id.calculate);


        ad.setCancelable(true); // This blocks the 'BACK' button  
        ad.setButton("Calculate", new DialogInterface.OnClickListener() 
        {  
        @Override  
        public void onClick(DialogInterface dialog, int which) 
        {  
            try {

            EditText rate=(EditText)findViewById((R.id.rate));
            CharSequence ra=rate.getText().toString();
            System.out.println("rate;"+ra);
            dialog.dismiss();                      
            }
                            catch(Exception e) 
            {
                e.printStackTrace();
                System.out.println("error::"+e);
            }
       }  
     });  
    ad.show();

}

rah