I have been trying to show a alert dialog for one of my projects, so here is how you do it...
package <your package name>;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Registration extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);//this is the XML file that you will using for your layout
Button button = (Button)findViewById(R.id.ok);
button.setOnClickListener(clicklistener);
}
public OnClickListener clicklistener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.create();
builder.show();
}
};
}
No comments:
Post a Comment