Wednesday, March 27, 2013
Dialog Box with Positive and Negative Button In Android
Android Supports many kind of Dialog Boxes, In this post I tried to explain Dialog Box with a XML EditText, a Negative and a Positive Buttons as shown in Fig., In further post I'll try to explain some other kind of Dialog box. Here is code for Dialog Box --->
/* ========================Code goes Here!! ===============================*/
public class DialogActivity extends Activity {
private static final int DLG_EXAMPLE1=0;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate (savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.ADD_Location );
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DLG_EXAMPLE1);
}});
}
// Method for Creating Dialog box
@Override
protected Dialog onCreateDialog(int id ){
switch(id){
case DLG_EXAMPLE1:
return createExampleDialog(); // Function call, body is defined below
default :
return null;
}
}
// This method is used for setting the dialogbox
@Override
protected void onPrepareDialog(int id,Dialog loginPrompt){
switch(id){
case DLG_EXAMPLE1:
final EditText first = (EditText)loginPrompt.findViewById(R.id.rf1);
first.setText(""); // setting the editbox blank
final EditText second = (EditText)loginPrompt.findViewById(R.id.rf2);
second.setText("");
break;
}
}
//Body the function createExampleDialog()
private Dialog createExampleDialog(){
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.userpasslayout, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Register Yourself!!");
alert.setIcon(R.drawable.logo);
alert.setMessage("Fill information carefully!");
alert.setView(textEntryView);
AlertDialog loginPrompt = alert.create();
//PositiveButton Onclick Listener
alert.setPositiveButton("Register", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "So you think!", Toast.LENGTH_LONG).show();
return;
}
});
//NegativeButton onclick Listener
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// NegtiveButton works automatic it will perform cancel of the dialog box;
return;
}
});
return alert.create();
}
/*=====================================================================*/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment