MY mENU


Monday, 21 January 2013

Creating Alert dialogue boxes in Android:

public class ButtoneventActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
                private Button b1;
                private Button b2;
                private Button b3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1=(Button)findViewById(R.id.button1);
        b1.setText("Click");
        b1.setOnClickListener(this);
        b2=(Button)findViewById(R.id.button2);
        b2.setText("alert");
        b2.setOnClickListener(this);
        b3=(Button)findViewById(R.id.button3);
        b3.setText("Conform");
        b3.setOnClickListener(this);
    }
                @Override
                public void onClick(View view) {
                                // TODO Auto-generated method stub
                                if(view==b1){
                                                AlertDialog showAlert=new AlertDialog.Builder(this).create();
                                                 showAlert.setTitle("it's clicked");
                                                 showAlert.setMessage("are you sure you want to Burn?");
                                                 showAlert.setButton("Burn",new OnClickListener(){
                                                                @Override
                                              public void onClick(DialogInterface dialog, int which) {
                                                                   // TODO Auto-generated method stub
                                                                                dialog.cancel();
                                                                }                                                             
                                                  });
                                                 showAlert.show();                         
                                        }
                                else if(view==b2){
                                               AlertDialog showAlert=new AlertDialog.Builder(this).create();
                                                 showAlert.setTitle("Button clicked");
                                                 showAlert.setMessage("are you sure you want to save?");
                                                 showAlert.setButton("save",new OnClickListener() {
                                                                @Override
                                                 public void onClick(DialogInterface dialog, int which) {
                                                                    // TODO Auto-generated method stub
                                                                                 dialog.cancel();
                                                                }       
                                       });
                                                 showAlert.setButton2("Cancle",new OnClickListener(){
                                                                                @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                                  //TODO Auto-generated method stub
                                                                                                 dialog.cancel();
                                                                                                                                                                                                       
                                       }                                 
                             });
                                                 showAlert.show();
                                }
                                else if(view==b3){
                                                AlertDialog showAlert=new AlertDialog.Builder(this).create();
                                                 showAlert.setTitle("Button clicked");
                                                 showAlert.setMessage("are you sure you want to save?");
                                                 showAlert.setButton("Yes",new OnClickListener(){
                                                                @Override
                                                  public void onClick(DialogInterface dialog, int which) {
                                                                        // TODO Auto-generated method stub
                                                                                 dialog.cancel();
                                                                }               
                               });
                                                 showAlert.setButton2("NO",new OnClickListener(){
                                                                                @Override
                                                      public void onClick(DialogInterface dialog, int which) {
                                                                               // TODO Auto-generated method stub
                                                                                                 dialog.cancel();               
                                                                                }     
                                                           });
                                                 showAlert.setButton3("Cancle",new OnClickListener(){
                                                                                @Override
                                     public void onClick(DialogInterface dialog, int which) {
                                                                // TODO Auto-generated method stub
                                                                                                 dialog.cancel();
                                                                                }      
                                        });  
                                  showAlert.show();   
                                               }
                             }
    }

No comments:

Post a Comment