Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the theme for DialogFragment

Tags:

Can someone please explain why this statement works perfectly well:

setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo); 

and the next statement does not deliver

setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog); 

This is what I have in the style's department:

<style     name="dialog">     <!-- title encapsulating main part (backgroud) of custom alertdialog -->     <item         name="android:windowFrame">@null</item>         <!-- turn off any drawable used to draw a frame on the window -->     <item         name="android:windowBackground">@null</item>         <!-- turn off any drawable used to draw a frame on the window -->     <item         name="android:windowIsFloating">true</item>         <!-- float the window so it does not fill the screen -->     <item         name="android:windowNoTitle">true</item>         <!-- remove the title bar we make our own-->     <item         name="android:windowContentOverlay">@null</item>         <!-- remove the shadow from under the title bar --> </style> 
like image 986
PageMaker Avatar asked Jun 14 '13 17:06

PageMaker


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?

Show activity on this post. 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 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.

How do you finish DialogFragment?

Show activity on this post. 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.


2 Answers

Try using the code in onCreate of the fragment as

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);              setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog); } 
like image 125
idle_developer Avatar answered Sep 17 '22 15:09

idle_developer


https://stackoverflow.com/a/24375599/2898715 it's also possible to omit the setStyle call altogether, and define the defulat style that DialogFragments will use throughout the whole app.

styles.xml

<?xml version="1.0" encoding="utf-8"?> <resources>      <style name="AppTheme" parent="android:Theme.Holo.Light">         <!-- makes all dialogs in your app use your dialog theme by default -->         <item name="alertDialogTheme">@style/DialogTheme</item>         <item name="android:alertDialogTheme">?alertDialogTheme</item>         <item name="dialogTheme">?alertDialogTheme</item>         <item name="android:dialogTheme">?alertDialogTheme</item>     </style>      <!-- define your dialog theme -->     <style name="DialogTheme" parent="Theme.AppCompat.DayNight.Dialog.MinWidth">         <item name="android:buttonStyle">@style/button_green</item>         <item name="android:textColorHint">@color/green</item>     </style>  </resources> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android">     .....     <application android:theme="@style/AppTheme">         .....     </application> </manifest> 
like image 27
Eric Avatar answered Sep 18 '22 15:09

Eric