tchike
tchike

Reputation: 164

AlertDialog after clicking on a button

I would like to add a AlertDialog after clicking on a button (cf below) that starts an activity to choose a picture.

I click the button to choose a picture. I choose the picture and I want to make display my AlertDialog after choosing the picture.

    final Button myButton = (Button) findViewById(R.id.button);
    myButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MyActivity.this, OpenImageActivity.class);
            startActivity(intent);
        }
    });

How can i make visible my AlertDialog?

Thanks.

Upvotes: 0

Views: 1032

Answers (3)

Jeffy Lazar
Jeffy Lazar

Reputation: 1923

you can do something like this

mButton1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

     confirmdialog();
}
 });

Now the confirmdialog Method

protected void confirmdialog() {
    // TODO Auto-generated method stub
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

    // Setting Dialog Title
    alertDialog.setTitle("Confirm Delete...");

    // Setting Dialog Message
    alertDialog.setMessage("Delete from History?");



    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
                         //whatever you want to do
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                         //cancel the dialog
            dialog.cancel();
        }
    });


    alertDialog.show();
}

Upvotes: 0

Saj
Saj

Reputation: 839

First , you have to sent intent like this

final Button myButton = (Button) findViewById(R.id.wall);
myButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intentCROP = new Intent(MyActivity.this, OpenImageListActivity.class);
        //startActivity(intentCROP);
        startActivityForResult(intentCROP, 1);
    }
});

then while you click on your list item on target activity

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Intent intent = new Intent();
    intent.putExtra("code", "response");
    setResult(RESULT_OK, intent);
    finish();

}

Now, back to first activity

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {


                            // here you can show your alert dialog


        }
    }

}

Upvotes: 1

Addicted Manish
Addicted Manish

Reputation: 189

AlertDialog.Builder alert;
alert = new AlertDialog.Builder(this);

& in your on click method do this

alert.setMessage("Title").setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                //do your work      

            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                dialog.cancel();
            }
        });

         AlertDialog alert1 = alert.create();  
         alert1.show();

Upvotes: 0

Related Questions