user3383415
user3383415

Reputation: 435

Call dialog Android

I have a dialog like this in my app:

//Dialog de idiomas 
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.language_prompt)
               .setItems(R.array.languages, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                   // The 'which' argument contains the index position
                   // of the selected item
                       switch (which) {
                            case 0:

                             savePreferences("idioma","es");
                             break;
                            case 1:
                              savePreferences("idioma","en");
                              break;

                       }
               }
        });
        return builder.create();

How can I call to show this dialog from a click event? Thank you

Upvotes: 0

Views: 3493

Answers (2)

learner
learner

Reputation: 3110

    final Context context = this;
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        // set title
        alertDialogBuilder.setTitle("");
        // set dialog message
        alertDialogBuilder
        .setMessage("Alert box title")
        .setCancelable(false)
        .setPositiveButton("es",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
         // if this button is clicked, close
        // current activity
 savePreferences("idioma","es");


                    })
                    .setNegativeButton("en",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        savePreferences("idioma","en");
                    }
                });

        // create alert dialog
        alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

Upvotes: 0

Bharath Mg
Bharath Mg

Reputation: 1127

Create an instance of this class fragment and call show() on that Object.

For your ref : http://developer.android.com/intl/es/guide/topics/ui/dialogs.html

Upvotes: 1

Related Questions