0

I have create one Alert Dialog which have two EditText and two Button and I fetch the value of the EditText and if value matches then I am doing some operation otherwise I want to call same AlertDialog again.But if value differs then I am not able to call the same alert dialog.I don't know where i am doing wrong...

My Code goes like this ::

public class MainActivity extends Activity 
{
private Button btn_click;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn_click=(Button)findViewById(R.id.btn_click);
    btn_click.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
            showDialog(0);
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) 
{
    switch (id) 
    {
    case 0:
        // This example shows how to add a custom layout to an AlertDialog
        android.app.AlertDialog.Builder login = new android.app.AlertDialog.Builder(this);

        try
        {

            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.login_dialog, null);

            final EditText username_alert = (EditText) textEntryView.findViewById(R.id.username);
            final EditText password_alert = (EditText) textEntryView.findViewById(R.id.password);

            login.setTitle("Login").setIcon(R.drawable.ic_launcher).setView(textEntryView)

            .setPositiveButton("Login", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int whichButton) 
                {
                    /* User clicked OK so do some stuff */
                    String uname_alert=username_alert.getText().toString();
                    String pass_alert=password_alert.getText().toString();

                    if(uname_alert.equals("aaaa") && pass_alert.equals("aaaa"))
                    {
                        //do Something........
                    }
                    else
                    {
                        showDialog(0);
                    }
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int whichButton) 
                {
                    /* User clicked cancel so do some stuff */
                }
            });
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return login.create();
    }
    return null;
}
  }

Hope my question is clear....... Please help me .. Thanks in Advance ..... :)

Akshay
  • 2,506
  • 4
  • 34
  • 55
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
  • you probably need to dismiss you dialog in the onclick ? – njzk2 Aug 24 '12 at 13:02
  • You can call the function that create and show your AlertDialog when you dismiss your dialog like this http://stackoverflow.com/a/42132765/5381331 – Linh Feb 09 '17 at 09:49

5 Answers5

0

Befor calling showDialog from dialog you should dismiss the existing dialog?

MemLeak
  • 4,456
  • 4
  • 45
  • 84
0

Currently you are creating new dialog every time.

try to keep the dialog you created once to a class level variable and reuse it. Like :

First time
dialog=login.create(); return dialog; Next time onwards return dialog only

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
0

I think you are trying to create AlertDialog inside AlertDialog which is not possible.Instead of that you can use Toast to show alert message.

Read this post for custom AlertDialog

Akshay
  • 2,506
  • 4
  • 34
  • 55
  • This actually isn't true. The Activity manages calls to `showDialog()` so this doesn't happen. – devunwired Aug 24 '12 at 13:21
  • @Devunwired Thanks for the info. Actually I had tried it but not succeeded and I had read somewhere in SO that it is not possible.Is it really possible? – Akshay Aug 24 '12 at 13:30
0

The issue is order of operations. Your click handler calls showDialog() before the dialog is dismissed, so that call does nothing. Then immediately after the handler method returns, AlertDialog calls dismiss() on itself (the default behavior) and disappears.

A better implementation of this feature would be to customize AlertDialog and intercept the button clicks before the DialogInterface handler is called so you can do your validation and not dismiss the dialog at all in that case, rather than trying to make it reappear. Truth is AlertDialog isn't well equipped to handle this case, and the best solution here is to not try to fit the square peg into the round hole and probably just do a custom Dialog implementation that does everything you need.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • if you can provide any example,it would be great ... :-) – AndroidLearner Aug 24 '12 at 14:47
  • This issue has been covered extensively on SO. Here are just a few solutions others have posted: http://stackoverflow.com/questions/4016313/how-to-keep-an-alertdialog-open-after-button-onclick-is-fired or http://stackoverflow.com/questions/6275677/alert-dialog-in-android-should-not-dismiss – devunwired Aug 24 '12 at 15:06
0

I know this answare is late. But for the future seekers. I had the same problem and no combination with .hide() .show() and .dismiss() doesn't react. Probably the system need some time and then it is working fine.

if (!SDCartConnected())
{
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable(){
            public void run() {
                showDialog(Const.DIALOG_SDCARD_MISSING);}}, 2000);  
}
vlkpo
  • 199
  • 1
  • 15