How to put two buttons in an alert box
I have created a simple alert box program in android.now I need to put two OK and Cancel buttons, but when I run the program it only shows the Cancel button ... my code is like below:
Main.java
public class MainActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
Toast.makeText(getApplicationContext(), "well come", 1).show();
}
});
alertDialog.setButton("cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
}
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
// see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button
}
});
}
}
thank you in advance.
source to share
I had the same problem. This is what has been done.
public class MainActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "well come", 1).show();
}
});
alertDialog.setButton2("cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
}
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
// see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button
}
});
}
}
This works great. You must dial the buttons for the warning.
source to share
change like this: setPositiveButton
ok
and setNegativeButton
there are buttons cancel
.
final AlertDialog.Builder alertDialog= new AlertDialog.Builder(this);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
Toast.makeText(getApplicationContext(), "well come", 1).show();
}
});
alertDialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
}
});
source to share
Correct way to add buttons:
For positive
alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// perform your action
}
});
Negative
alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
source to share
Simple alert
private AlertDialog AskOption()
{
AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this)
//set message, title, and icon
.setTitle("Title")
.setMessage("Message")
.setIcon(R.drawable.icon)
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//your code
}
})
.setNeutralButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.create();
return myQuittingDialogBox;
}
Using
AlertDialog al = AskOption();
al.show();
source to share
public void showDialog(Activity activity, String title, CharSequence message) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
if (title != null)
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
}
source to share
You can use the AlertDialog.Builder class. There are several ways to place a button on a dialog using this builder class.
source to share
you need to use setPositiveButton () and setnegativeButton () like,
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Toast.makeText(getApplicationContext(), "well come", 1).show();
}
});
alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
}
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
// see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button
}
});
}
}
source to share