Reject DialogFragment onClick

I tried to create a DialogFragment dialog box that could be canceled when listening, after some searching, I decided to go with this implementation:

public class ErrorDialogFragment extends RoboDialogFragment { private static final String MESSAGE_ARG = "message"; private TextView text; public ErrorDialogFragment newInstance (String message){ ErrorDialogFragment f = new ErrorDialogFragment(); Bundle args = new Bundle(); args.putString(MESSAGE_ARG, message); f.setArguments(args); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.error_dialog_fragment, container, false); v.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ErrorDialogFragment.this.dismiss(); } }); text = (TextView) v.findViewById(R.id.error_dialog_text_textView); text.setText(getArguments().getString(MESSAGE_ARG)); return v; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NO_TITLE, 0); } 

A special message can be configured in the alert dialog box and will be rejected when clicked.

What do you think is the best way to achieve this?

Thanks.

+8
android android-fragments dialog
source share
3 answers

You can use dialog.setCanceledOnTouchOutside (true); that closes the dialog if you touch outside the dialog box. or

Try this tutorial http://iserveandroid.blogspot.com/2010/11/how-to-dismiss-custom-dialog-based-on.html . Hope this helps .. !!

+14
source share
 final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle(title); alertDialog.setMessage(msg); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); } }); alertDialog.setIcon(R.drawable.error_icon); alertDialog.show(); 

just use this code whenever you want to show a warning and its ok onclick event dialog will be rejected.

+2
source share

I call DialogFragment from Activity. After clicking the button in the dialog box, I use the interface to call the method inside the action. In this exercise, I do the following:

 // This is the code inside the activity that call the dialog Fragment fragment = getSupportFragmentManager().findFragmentByTag("MyDialog"); if(fragment != null) { DialogFragment dialog = (DialogFragment) fragment; dialog.dismiss(); } 
0
source share

All Articles