I use this to create a true custom dialog box.
This requires a layout called alert_dialog_view
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.alert_dialog_view, null);
//Set the view
alert.setView(alertView);
//Show alert
final AlertDialog alertDialog = alert.show();
//OPTIONAL Can not close the alert by touching outside.
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
To create a standard dialog box, you can do this...
public class DialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
toast.makeText(this,"enter a text here",Toast.LENTH_SHORT).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
});
// Create the AlertDialog object and return it
return builder.create();
}
}
}