Calling DialogFragment from Fragment (not FragmentActivity)?
Check for import statements. If we use
ExampleDialogFragment dialog = new ExampleDialogFragment ();
dialog .show(getFragmentManager(), "example");
Then make sure to import
import android.app.DialogFragment;
import android.app.Fragment;
not from the support library.
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
When you create a new Dialog
, you can simply call it using this (very) simple method from a Fragment
.
DialogFragment dialog = DialogFragment.instantiate(getActivity(), "Hello world");
dialog.show(getFragmentManager(), "dialog");
If you want to use your own dialog, please use that kind of code.
public class MyDialogFragment extends DialogFragment
{
//private View pic;
public MyDialogFragment()
{
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_my_dialog, new LinearLayout(getActivity()), false);
// Retrieve layout elements
TextView title = (TextView) view.findViewById(R.id.text_title);
// Set values
title.setText("Not perfect yet");
// Build dialog
Dialog builder = new Dialog(getActivity());
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setContentView(view);
return builder;
}
}
it will help if you need to show a fragment dialog inside a fragment
DialogFragment
public class DialogBoxFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("simple dialog");
return rootView;
}
}
Now showing the fragment dialog into a fragment
DialogFragment dialogFragment = new DialogFragment ();
dialogFragment.show(getActivity().getFragmentManager(),"simple dialog");