I understand that there is this documentation
http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
but as a new Android/Java le
Alert with custom view
public class MyAlertDialogFragment extends DialogFragment {
public static final String TITLE = "dataKey";
public static MyAlertDialogFragment newInstance(String dataToShow) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putString(TITLE, dataToShow);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.alert_layout, null);
TextView mTextView = (TextView) view.findViewById(R.id.textview);
mTextView.setText(mDataRecieved);
setCancelable(false);
builder.setView(view);
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
}
And Alert with YesNoDialog interface
public class MyAlertDialogFragment extends DialogFragment {
public static final String TITLE = "dataKey";
private OnYesNoClick yesNoClick;
public static MyAlertDialogFragment newInstance(String dataToShow ) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putString(TITLE, dataToShow);
frag.setArguments(args);
return frag;
}
public void setOnYesNoClick(OnYesNoClick yesNoClick) {
this.yesNoClick = yesNoClick;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setMessage("Message to Show")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if(yesNoClick != null)
yesNoClick.onNoClicked();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if(yesNoClick != null)
yesNoClick.onYesClicked();
}
});
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
public interface OnYesNoClick{
void onYesClicked();
void onNoClicked();
}
}
Use it like
private void showYesNoDialog(){
MyAlertDialogFragment yesNoAlert = MyAlertDialogFragment.newInstance(
"Data to Send");
yesNoAlert.show(getFragmentManager(), "yesNoAlert");
yesNoAlert.setOnYesNoClick(new MyAlertDialogFragment.OnYesNoClick() {
@Override
public void onYesClicked() {
//yes or ok clicked
}
@Override
public void onNoClicked() {
//no or cancel clicked
}
});
}