Android Dialog Ready for use For Utility with custom message and text for buttons

Android Dialog Ready for use For Utility with Custom message and text for Yes, No buttons.

Create Class ConfirmCustomDialog

package com.kp.core;

import com.kp.dialogs.IDialogCallback;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;

public class ConfirmCustomDialog extends DialogFragment {
CharSequence title,message;
String yesText=””;
String noText=””;
IDialogCallback dialogCallback;

public ConfirmCustomDialog(CharSequence title,CharSequence message,String yesText,String noText,IDialogCallback dialogCallback ) {
this.title = title;
this.message = message;
this.dialogCallback = dialogCallback;
this.yesText = yesText;
this.noText = noText;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle(title)
.setMessage(message)
.setNegativeButton(noText, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialogCallback.onDialogNoClick();
}
})
.setPositiveButton(yesText, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialogCallback.onDialogYesClick();
}
}).create();
}
}

Create IDialogCallback Interface

package com.kp.dialogs;

public interface IDialogCallback {
public void onDialogYesClick();
public void onDialogNoClick();
}

Code Use in Activity To show Dialog

ConfirmCustomDialog newFragment = new ConfirmCustomDialog(“Confirm”, Html.fromHtml(status_msg),”Save Anyway”,”Cancel” ,new IDialogCallback() {
@Override
public void onDialogYesClick() {
saveanywayflag=”1″;
createNewEvent();
}
@Override
public void onDialogNoClick() {
// Toast.makeText(activity, ” Saved Successfully. ” ,Toast.LENGTH_SHORT).show();
// saveTaskDetail();
}
});
newFragment.show(getFragmentManager(), “dialog”);

Add a Comment