5

I'm trying to style all my dialog fragments to look the same in my app. The dialogs coming from my settings fragment are styled exactly the way I want it. For my custom dialog fragments, the style is similar but not exactly the same. For some reason the spinner, timepicker, datepicker, radiobuttons, and edittext widgets inside my custom dialog fragments don't pick up the same style. In fact, the widgets blend in with the white background and you can't see that they are there. What am I doing wrong?

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"/>

<style name="Theme.Base" parent="AppTheme">
    <item name="colorPrimary">@color/PrimaryBackgroundColor</item>
    <item name="colorPrimaryDark">@color/SecondaryBackgroundColor</item>
    <item name="colorAccent">@color/ColorBackgroundAccent</item>
    <item name="android:textColorPrimary">@color/PrimaryTextColor</item>
    <item name="android:alertDialogTheme">@style/AppTheme.DialogStyle</item>
</style>
    <style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColorPrimary">@color/PrimaryBackgroundColor</item>
    <item name="colorAccent">@color/ColorBackgroundAccent</item>
</style>

I'm applying the theme to my custom dialog fragment like this:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_DialogStyle);

As you can see, the radio button selected color red and you can't see the unselected radio button.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
TrackDave
  • 279
  • 2
  • 14
  • Both the screenshots you provided look similar apart from the radio button accent color. What exactly do you want to change in the custom dialog? – jaibatrik Sep 25 '15 at 14:10
  • I want the color of the radio buttons to match and also, my other dialogfragment with timepicker, datepicker, and spinner are blending in with the background for some reason, so the user won't be able to see them. – TrackDave Sep 25 '15 at 14:44

4 Answers4

21

Finally got an answer!!!

It's an issue or bug with AppCompat 22+. Check out link here

Apparently this was a bug with fragments and widgets weren't getting the material themed in a fragment. It seems they fixed this issue, but the issue still holds in a dialog fragment based on what I'm going through. The problem comes when you use the inflater instance passed to Fragment#onCreateView(). The workaround for now is to instead used the LayoutInflater from getActivity().getLayoutInflater() according to google.

So I changed my code to:

View view = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);

from:

View view = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.dialoge, null);

All my widgets are now themed. Thanks everyone. Hopes this helps someone else.

Owen S.
  • 7,665
  • 1
  • 28
  • 44
TrackDave
  • 279
  • 2
  • 14
  • 1
    ugh...can't believe it's still not fixed in the 23.4.0 support libs :-( – kenyee Aug 01 '16 at 17:48
  • I'm going to print out this question and answer and wallpaper my room with it. Thank you for getting back! I'm glad I found this after an hour, not three days... – oli.G Apr 05 '17 at 22:56
  • Issue is still not fixed and the workaround you specified worked. Although I would suggest to use inflater from Activity directly instead of using Application wise inflator. – muthuraj Jun 08 '20 at 10:55
0

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)
Kassisdion
  • 345
  • 3
  • 13
  • Florian, that's exactly what I have in my post AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_DialogStyle); – TrackDave Sep 25 '15 at 14:41
  • I've facing the same issue when i was trying to make a custom dialog with a ratingBar. (if i good remember there is somethink special under the dialog and maybe u'll have to custom the ratingBar, i'll check this out) – Kassisdion Sep 25 '15 at 16:17
  • Thanks a lot. Let me know. – TrackDave Sep 25 '15 at 16:22
0

I think you need to add one more item in style of your dialog. android:textColorSecondary will show color of un selected checkbox.

in your style add it.

</style>
    <style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColorPrimary">@color/PrimaryBackgroundColor</item>
    <item name="colorAccent">@color/ColorBackgroundAccent</item>
     <item name="android:textColorSecondary">#000000</item>
</style>

It will make un Checked checkbox or toggle button edge color black. you need to change #000000 to color your want to show.

Shvet
  • 1,159
  • 1
  • 18
  • 30
0

See if this helps - Android appcompat-v7:21.0.0 change material checkbox colors

In short, try setting android:textColorSecondary.

Community
  • 1
  • 1
headuck
  • 2,763
  • 16
  • 19
  • I did try that. I put that in my dialogtheme and apptheme, but didn't work. It seems that my appTheme doesn't apply to any widgets in my custom dialog fragment – TrackDave Sep 25 '15 at 15:03
  • How about `android:textColorSecondaryInverse`? – headuck Sep 25 '15 at 15:11
  • Nothing unfortunately :( – TrackDave Sep 25 '15 at 15:20
  • Just in case, are you using appcompat 22.1 or later, and import from android.support.v7.app.AlertDialog? – headuck Sep 25 '15 at 15:30
  • com.android.support:appcompat-v7:23.0.1 and yes I'm importing from v7 – TrackDave Sep 25 '15 at 15:34
  • Perhaps you can try your luck with the answer by neoteknic http://stackoverflow.com/questions/29797134/how-to-use-and-style-the-new-alertdialog-from-appcompat-22-1 (use xml rather than the second parameter to style Dialog)? – headuck Sep 25 '15 at 15:40
  • I'm thinking because I'm using a custom view for my dialog fragment, I may need to style each of the widgets inside the layout. Thanks for the help :) – TrackDave Sep 25 '15 at 15:46