I have made a program of simple alert box in android.now i have to put two buttons “OK” and “cancel” but when i run the program it only shows ,the “cancel” button…my code is as 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.
I had the same problem. This is what is did.
This works fine. You have to number the buttons for alert-box.