DialogFragment (Simple Dialog)
From ICS(Honey Comb for Tablet), Fragment becomes general use UI parts for Android.
DialogFragment is also included in Fragment family.
How to make Simple Dialog
Fragment
public class SimpleDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Resources res = getActivity().getResources(); Bundle bundle = getArguments(); AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); dialog.setTitle("Test"); dialog.setPositiveButton("OK", new PositiveButtonClickListener()); dialog.setNegativeButton("Cancel", new NegativeButtonClickListener()); return dialog.create(); } class PositiveButtonClickListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } } class NegativeButtonClickListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } } }
Call from Activity
FragmentManager manager = getFragmentManager(); SimpleDialogFragment dialog = new SimpleDialogFragment(); dialog.show(manager, "Dialog");
Results
When opening dialog, background color changes.
To close dialog, you touch outside of dialog, dialog closed.
Close Dialog
dialog.dismiss();
How to get Resource to set Text as Resource
Resources res = getActivity().getResources();