-1

i have custom dialog with three button . i have click listener for 3 buttons.... here is the code.

 public void addDialog() {
        // TODO Auto-generated method stub
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            // Setting Dialog Title
            alertDialog.setTitle("Add From");
            // Setting Dialog Message
            alertDialog.setMessage("Add Number: ");
            LayoutInflater layoutInflater 
            = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view=layoutInflater.inflate(R.layout.dialog_lay,null);

           Button btn_Contact = (Button)view.findViewById(R.id.btn_contact);
           Button btn_SMS = (Button)view.findViewById(R.id.btn_sms);
           Button btn_Manually = (Button)view.findViewById(R.id.btn_manually);
           //  Setting Negative "NO" Button
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                // Write your code here to invoke NO event
                Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                dialog.cancel();
                }
            });

            OnClickListener listenerDial = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
            // i want to close alert dialog here    

            Toast.makeText(getApplicationContext(), "You clicked contact btn", Toast.LENGTH_SHORT).show();

                }
            };

            // add listener to button.
            btn_Contact.setOnClickListener(listenerDial);
            btn_SMS.setOnClickListener(listenerDial);
            btn_Manually.setOnClickListener(listenerDial);
            alertDialog.setView(view);
            alertDialog.show();

    }

i want to close this alert dialog when any of the three key is pressed .. any suggetion plzzzz..

Abdus Salam
  • 55
  • 1
  • 11

4 Answers4

0

Use alertDialog.dismiss() to close it.

baldguy
  • 2,090
  • 1
  • 16
  • 25
0
OnClickListener listenerDial = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
        // i want to close alert dialog here    

        alertDialog.dismiss();
        Toast.makeText(getApplicationContext(), "You clicked contact btn",        
        Toast.LENGTH_SHORT).show();

            }
        };
duggu
  • 37,851
  • 12
  • 116
  • 113
0

i'v modified your code just check it out

 public void addDialog() {
    // TODO Auto-generated method stub
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        // Setting Dialog Title
        alertDialog.setTitle("Add From");
        // Setting Dialog Message
        alertDialog.setMessage("Add Number: ");
        LayoutInflater layoutInflater 
        = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.dialog_lay,null);

       Button btn_Contact = (Button)view.findViewById(R.id.btn_contact);
       Button btn_SMS = (Button)findViewById(R.id.btn_sms);
       Button btn_Manually = (Button)findViewById(R.id.btn_manually);
       //  Setting Negative "NO" Button
        alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event
            Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
            // dialog.dimiss(); // dialog will dismiss when you click on this button un-comment it so it works.
            }
        });

        OnClickListener listenerDial = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            dialog.dimiss(); // dialog will dismiss when you click on this button

        Toast.makeText(getApplicationContext(), "You clicked contact btn", Toast.LENGTH_SHORT).show();

            }
        };

        // add listener to button.
        btn_Contact.setOnClickListener(listenerDial);
        btn_SMS.setOnClickListener(listenerDial);
        btn_Manually.setOnClickListener(listenerDial);
        alertDialog.setView(view);
        alertDialog.show();

}
Kosh
  • 6,140
  • 3
  • 36
  • 67
  • dialog reference is not defined . – Abdus Salam Feb 23 '13 at 09:45
  • in your button findViewById u have to do it like this alertDialog.findViewById this will reference the buttons to the dialog. try it and if it didn't work could you post the logcat! – Kosh Feb 23 '13 at 11:01
0

Just Added

final AlertDialog Dial = alertDialog.create(); 

and change

dialog.setView(layout); to Dial.setView(layout);

now just call Dial.dismiss(); in onclick listener.. works fine for me.

Abdus Salam
  • 55
  • 1
  • 11