Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theme not applying to DialogFragment on Android

I'm switching my old Dialogs to DialogFragment, but the themes and styles don't seem to be working.

I'm using the DialogFragment from the compatibility library v4, and in the onCreate method I've tried calling setStyle(style, theme); with a lot of different themes, but the dialog always shows as an "old" dialog in the emulator running Android 4.0.3 (i.e., it does not shows in Holo theme).

Is there anything else that I should be doing? Does using the compatibility library disables the Holo theme or anything? If this is the case, should I create two DialogFragments, one for older versions and one for newer versions?

Thanks!


Here's the (simplified) code for my dialog. I've tried both Theme_Holo_Dialog_NoActionBar and Theme_DeviceDefault_Dialog_NoActionBar, but the Android 4 emulator always shows the dialog as an "old" dialog instead of using the Holo theme. What am I doing wrong? :(

[...] import android.support.v4.app.DialogFragment; [...]  public class AlertDialogFragment extends DialogFragment {    public static AlertDialogFragment newInstance(int id) {      AlertDialogFragment f = new AlertDialogFragment();     Bundle args = new Bundle();     args.putInt("id", id);     f.setArguments(args);   }    @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     int style = DialogFragment.STYLE_NORMAL, theme = 0;     theme = android.R.style.Theme_Holo_Dialog_NoActionBar;     setStyle(style, theme);        }    @Override   public Dialog onCreateDialog(Bundle savedInstanceState) {      mId = getArguments().getInt("id");     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())         .setTitle(mTitle)         .setMessage(mMessage)         .setPositiveButton(getString(R.string.btn_ok), new DialogInterface.OnClickListener() {                   @Override             public void onClick(DialogInterface dialog, int which) {                 dismiss();                               }         });         return builder.create();     } 
like image 680
luthier Avatar asked May 14 '12 21:05

luthier


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I destroy DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.

How do I know if DialogFragment is showing?

This is gonna be disappointing to you "great" finding but just call progressDialog. showDialog() twice back-to-back and you will get two dialogs. Because show is asynchronous and your findFragmentByTag(TAG) == null check will be true until dialog is actually added by system.


2 Answers

You shoudn't use the AlertDialog.Builder(Context, int) constructor with the support library because it is only available since API 11.

To setup a theme to your dialog, use instead a ContextThemeWrapper like this:

ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar); AlertDialog.Builder builder = new AlertDialog.Builder(context); 
like image 70
Florian Avatar answered Oct 16 '22 20:10

Florian


I believe you need to set the theme on the actual Dialog and not the Fragment

Use this constructor to create your AlertDialog:

 AlertDialog.Builder(Context context, int theme) 

ie

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme) 
like image 33
Blundell Avatar answered Oct 16 '22 19:10

Blundell