Reputation: 3901
I have developed application in which I want to display Fragment as a dialog,
I used Tabs and Fragment in my application, I have just one activity and I replace the fragment as I need,
If we used activity then we declare "android:theme="@android:style/Theme.Dialog" in Manifest file to display activity as dialog, same thing I want to do for Fragment
Upvotes: 9
Views: 21908
Reputation: 1182
We can show a Fragment as a dialog using two means , but a single way.
Explanation:
Way:
Extend class DialogFragment and override any one of two methods:
onCreateView() OR
onCreateDialog().
Diff. between these two:
Overriding onCreateView() will let you show a Fragment as dialog and you can make the Title text customized.
On the other hand, overriding onCreateDialog(), you can again show a fragment as dialog and here you can customize the entire dialog fragment. Means, you can inflate any view to show as dialog.
If you need any source code explaining the above text, let me know.
Note:
Using DialogFragment has a disadvantage. It doesn't handle screen orientation. And crashes the app.
So, you need to use setRetainInstance() inside onCreate() of DialogFragment class.
Upvotes: 10
Reputation: 5628
This is a loading dialog that I use:
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.os.Bundle;
public class LoadingDialogFragment extends DialogFragment {
private final String message;
private LoadingDialogFragment(String message) {
this.message = message;
}
public static LoadingDialogFragment newInstance(String message) {
LoadingDialogFragment fragment = new LoadingDialogFragment(message);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage(message);
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
Can be instantiated like this:
LoadingDialogFragment.newInstance(context.getString(R.string.loading_message))
You can inflate views and setContentView from inside of this dialog if you want a custom layout. http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Upvotes: 0
Reputation: 4389
Just use a DialogFragment. It is the intended fragment subclass for this use.. http://developer.android.com/reference/android/app/DialogFragment.html
Upvotes: 0
Reputation: 353
Your fragment class should extend DialogFragment rather than fragment.
Check out the docs: http://developer.android.com/reference/android/app/DialogFragment.html
Upvotes: -3