Sunday 15 July 2012

AlertDialog box with Three buttons in Android



In this activity we using 3 buttons,which are NeturalButton,PositiveButton,NegativeButton.
Here also discussing the coding to dismiss the Activity,dialog box and how moving to next Activity.


The NeturalButton is using to dismiss the dialog box.



b.setNeutralButton("dismiss dialog", new OnClickListener() {

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

Here the PositiveButton button is using to move from one Activity to another.



b.setPositiveButton("go to next class", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i=new Intent(AlertDialogExpActivity.this,ss.class);
startActivity(i);
}
});

Here the NegativeButton is using to finish the activity.


b.setNegativeButton("finish class", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
AlertDialogExpActivity.this.finish();
}
});



package com.ann;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class AlertDialogExpActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void onClick(View v){
    Builder b=new AlertDialog.Builder(this);
    b.setTitle("AlertDialog with Three Buttons");
    b.setMessage("This is the messageee...!!!");
    b.setNeutralButton("dismiss dialog", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
    b.setPositiveButton("go to next class", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i=new Intent(AlertDialogExpActivity.this,ss.class);
startActivity(i);
}
});
    b.setNegativeButton("finish class", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
AlertDialogExpActivity.this.finish();
}
});
    AlertDialog d=b.create();
    d.show();
    }
}