I'm trying to change the text color in dialog boxes, most commonly AlertDialog. I've tried every solution at these pages:
AlertDialog styling - how to change style (color) of title, message, etc
How can I change the color of AlertDialog title and the color of the line under it
How to change theme for AlertDialog
Most solutions work BELOW 5.0 but above it, and they don't seem to have any effect. What different attributes should I be changing for 5.0+?
The parent theme of my app is "Theme.AppCompat.Light.NoActionBar"
In your case, I think the dialog theme would work.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.YourDialogStyle;
You can specify your dialog theme just like the code above.
<style name="YourDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="android:colorAccent">#color/primary</item>
<item name="android:textColor">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_dark</item>
</style>
Here is the example of theme for Dialog.
android:colorAccent will affect your text colors of negative or positive buttons.
android:textColor will affect your title text color of dialog.
android:textColorPrimary will affect your message color of dialog.
Another option is you can make your own custom layout for the dialog.
Related
if the app called AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); it is expect to show dark theme, and if not the app is with light theme.
Having AlertDialog.Builder(this), and would like to apply one theme so that in MODE_NIGHT it shows the dialog with dark theme, other wise the dialog shows with the light theme, something like below (but this android.R.style.Theme_Material_Dialog will causes the dialog to be always in dark theme)
AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog)
Is there one theme for the AlertDialog? or it has to define two themes and check the mode and then to code with the theme respectively?
Define your alert dialog in styles.xml
<style name="MyDialogStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>
In your code
AlertDialog.Builder(this, R.style.MyDialogStyle)
And give it a try!
add to your theme style:
<item name="alertDialogTheme">#style/Theme.AppCompat.DayNight.Dialog.Alert</item>
<item name="android:alertDialogTheme">#style/Theme.AppCompat.DayNight.Dialog.Alert</item>
use the dialog from appcompat
import androidx.appcompat.app.AlertDialog;
AlertDialog alertDialog = new AlertDialog.Builder(context).create()
I am building a dialog with DialogFragment, and I need to change the color of the shadow (mask), which is transparent and always around the main dialog content. Actually, almost all of the answers on the web is to change the dialog box color, but I need to change the shadow color from transparent black to others. Is there any way to do that?
image:
If you only want to change the alpha for the background then you can easily create a style with android:backgroundDimAmount attribute, like I did
<style name="CustomAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="android:backgroundDimAmount">0.5</item>
</style>
the value ranges 0-1 and then passing the style in your AlertDialog as
AlertDialog dialog = new AlertDialog.Builder(this, R.style.CustomAlertDialogStyle)
.setTitle("Title").setMessage("message").create();
dialog.show();
But if you want to change the background color as well then you can try this work around given by #Lee Han Kyeol
https://stackoverflow.com/a/29482234/5002610
I hope this will solve your issue.
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);
My settings dialog looks like this (Exactly how I want it):
Settings Dialog Fragment
My custom dialog fragment looks like this:
Custom Dialog Fragment
As you can see, the radio button selected color red and you can't see the unselected radio button.
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.
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)
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.
See if this helps -
Android appcompat-v7:21.0.0 change material checkbox colors
In short, try setting android:textColorSecondary.
Anyone had this issue, that after selecting text in dialog's EditText, toolbar appears with invisible action buttons (white on white background)?
How do I set theme to a DialogFragment?
You can custom dialog theme in styles.xml:
<style name="MyDialogTheme" parent="android:Theme.Holo.Dialog" />
then use MyDialogTheme in your DialogFragment
UPDATE
In your dialog use following codes to set custom theme:
ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), R.style.MyDialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
... ...
I have created custom style for checkbox
<style name="CheckBoxtpi" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">#drawable/tpi_btn_check_holo_light</item>
</style>
And apply it to my Theme, it works pretty, but all checkboxes in alert dialogs still standart Holo Blue color.
How can i change this?
There are two alternatives you can do;
Change your context from getApplicationContext() or any other to explicitly MyActivity.this, (whatever your activity name is) when you build your dialog.
This will probably solve the problem.
You may create a custom layout for the dialog box and call it
dialog.setContentView(R.layout.custom_dialog);