Reputation: 5027
I have updated the alert dialog box format using my own layout xml.
In this customerized alert dialog box there are 2 buttons, one button is to save the data being input, another one is a cancel button.
How could I write for the CANCEL button such that when the user click it, just simply to DISMISS the dialog box?
public OnClickListener NewRowButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(getLayoutInflater().inflate(R.layout.custom_dialog_add, null));
builder.create();
AlertDialog Custom_dialog_add = builder.create();
Custom_dialog_add.show(); // show the Dialog
Button CancelButton = (Button) findViewById(R.id.CancelButton);
CancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {Custom_dialog_add.cancel();} //WRONG: Cannot refer to a non-final variable Custom_dialog_add inside an inner class defined in a different method
});
}
};
it is now revised as follows:
public OnClickListener NewRowButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setView(getLayoutInflater().inflate(R.layout.custom_dialog_add, null));
dialog.create();
final AlertDialog test = dialog.create();
test.show();
Button close = (Button) findViewById(R.id.CancelButton);
close.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
test.dismiss();
}
});
}
};
The Eclipse does not report bugs to the revised codings, but when simulated, it runs out a nullpointer exception. The logcat is as follows. How could such be solved?
09-28 20:15:19.505: E/AndroidRuntime(25847): FATAL EXCEPTION: main
09-28 20:15:19.505: E/AndroidRuntime(25847): java.lang.NullPointerException
09-28 20:15:19.505: E/AndroidRuntime(25847): at com.pearappx.gamescore3.MainActivity$4.onClick(MainActivity.java:422)
09-28 20:15:19.505: E/AndroidRuntime(25847): at android.view.View.performClick(View.java:3627)
09-28 20:15:19.505: E/AndroidRuntime(25847): at android.view.View$PerformClick.run(View.java:14329)
09-28 20:15:19.505: E/AndroidRuntime(25847): at android.os.Handler.handleCallback(Handler.java:605)
09-28 20:15:19.505: E/AndroidRuntime(25847): at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 20:15:19.505: E/AndroidRuntime(25847): at android.os.Looper.loop(Looper.java:137)
09-28 20:15:19.505: E/AndroidRuntime(25847): at android.app.ActivityThread.main(ActivityThread.java:4511)
09-28 20:15:19.505: E/AndroidRuntime(25847): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 20:15:19.505: E/AndroidRuntime(25847): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 20:15:19.505: E/AndroidRuntime(25847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
09-28 20:15:19.505: E/AndroidRuntime(25847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
09-28 20:15:19.505: E/AndroidRuntime(25847): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 5830
Reputation: 8781
Would this code snippet help you?
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_layout, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("dialog");
dialog.setView(view);
final AlertDialog test = dialog.create();
Button close = (Button) view.findViewById(R.id.close_button);
close.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
test.dismiss();
}
});
Edit updated version:
//Create new alert dialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
//set title
dialog.setTitle("title");
//create the dialog in a final context
final AlertDialog test = dialog.create();
//inflate the custom layout in to a View object
View view = getLayoutInflater().inflate(R.layout.custom_dialog_add, null);
//find the Button object within the inflated view
// ↓↓↓
Button close = (Button) view.findViewById(R.id.CancelButton);
//set the onClickListener
close.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
test.dismiss();
}
});
//show the dialog
test.show();
Don't forget to use the right imports!
Upvotes: 2
Reputation: 7110
Are you using a seperate activity to lauch the layout as a dialog or any other way? If you are using seperate activity to lauch the layout
simply call
youractivity.this.finish()
in your cancel button onclick event
Upvotes: 0
Reputation: 2079
You can create the button by yourself. Set an onClickListener over that button and inside onClick() you can call dialog.cancel();
which simply cancels the dialog.
Upvotes: 0
Reputation: 544
This inside the dialog { } (ex: public void Dialog(...){ here.. }
DialogInterface.OnClickListener cancel = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
1 add = 1.this;
add.finish();
Intent showActivity = new Intent(1.this, 2.class);
1.this.startActivity(showActivity);
}
};
Edit these:
1 - name of the .java file you're at.
2 - name of the .java class you want to open/show when Dialog canceled.
Then put this code
builder.setNegativeButton("Cancel", cancel);
What will happen? when you click in the "Cancel" button it will close the "page(with dialog) and will open the activity/page you want.
or try this: dialogo.setNeutralButton("Cancel", null);
Upvotes: 0
Reputation: 1326
here an example:
// creates Dialogs for this Activity
@Override
protected Dialog onCreateDialog(int id) {
final Dialog dialog;
switch(id) {
case DIALOG_REALLY_EXIT_ID:
dialog = new AlertDialog.Builder(this).setMessage(
"Do you really want to exit this activity?")
.setTitle("Exit activity")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//add your code you would like to be execute when clicking "yes"
//for example the below to exit your activity
//Main.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //to dismiss this dialog
//add any additional things you would like to execute when pressing the no button
}
}).create();
break;
default:
dialog = null;
}
return dialog;
}
Upvotes: 0