Sunday, November 13, 2011

Android AlertDialog Example :-

Android AlertDialog Example :-
To ask user permission for "Exit ?" we can use Android AlertDialog with Yes or No Button. So, by clicking Yes we can Exit the Application  and by clicking No we need not to do any action.
[sourcecode language="java"]
public class ExampleApp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Do you want to close this window ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//  Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Title");
// Icon for AlertDialog
alert.setIcon(R.drawable.icon);
alert.show();
}
}
[/sourcecode]
The output will looks like
alertdialogexample

No comments:

Post a Comment

content -->