I'm trying to style the dialog box that appears when I select an EditTextPreference in my SettingsActivity. So far I have been able to change the colour of the two buttons and the background with:
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(0xffffffff);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(0xffffffff);
dialog.getWindow().setBackgroundDrawableResource(R.drawable.blue_background);
But I am so far unable to change the title at the top of the dialog.
The colour of the 'Zone' title is being retrieved from a style I use for the preference list, it's being inherited and I can't figure out how to assign it a style that will change that title colour
<style name="MyPreferenceTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#color/care_call_white</item>
<item name="android:textColor">#drawable/text_colour_state</item>
<item name="android:textColorSecondary">#drawable/text_colour_state</item>
</style>
Can anyone help with a style that could do this? Or with some code that could possibly set it instead? All my dialog boxes are styled the same but this one is trickier as it's part of the Preference so I can't customise it the same way I did with the others.
Just to clarify, the only thing that needs changing is the "Zone" title.. I would like it to be white.
With new AndroidX Preference library there is no onPrepareDialogBuilder in EditTextPreference.
I've proceeded in this way:
My base app theme:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
//add here your properties
</style>
my preference theme:
<style name="AppTheme.Settings">
<!-- This will change the opening dialogs for EditTextPreference -->
<item name="alertDialogStyle">#style/Theme.AlertDialog.Default</item>
</style>
in my case Theme.AlertDialog.Default is a custom Dialog theme with with parent ThemeOverlay.MaterialComponents.MaterialAlertDialog
Then in your AndroidManifest.xml simply apply the theme to the the Activity which handle the preferences
Sorted.
I created a CustomEditTextPreference class that inherited EditTextPreference and then overrode
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
builder.getContext().setTheme(R.style.PreferenceThemeDialog);
super.onPrepareDialogBuilder(builder);
}
and changed the style in the builders context with
<style name="PreferenceThemeDialog">
<item name="android:textColor">#color/care_call_white</item>
</style>
One way you can change the theme or the title of a preference is by finding it within your settings activity or fragment and then editing the attributes.
To edit the theme you would do this:
findPreference<EditTextPreference>(YOUR_KEY_GOES_HERE)?.context?.setTheme(YOUR.THEME.GOES.HERE)
To change the title you would do this:
findPreference<EditTextPreference>(BACKGROUND_SYNC_PERIOD_KEY)?.dialogTitle = YOUR_TITLE_GOES_HERE
Both of these are in kotlin but it is essentially the same thing in java.(you can paste this into android studio and it should format it).
<style name="cust_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
</style>
<style name="dialog_title_style" parent="android:Widget.TextView">
<item name="android:background">#android:color/black</item>
<item name="android:padding">10dp</item>
</style>
And you can instantiate dialog:
Dialog dialog=new Dialog(this,R.style.cust_dialog);
dialog.setContentView(R.layout.fragment_features_dialog);
dialog.setTitle(R.string.features);
Now the dialog shows up with black title background color.
Related
The common style for AlertDialogs is set in themes.xml
<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="alertDialogTheme">#style/ThemeOverlay.AlertDialog</item>
</style>
And the ThemeOverlay.AlertDialog looks like this:
<style name="ThemeOverlay.AlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
...
<item name="buttonStyle">#style/AlertDialogButton</item>
...
</style>
However for one particular dialog in the app I need to use a different buttonStyle (textColor). For this I've created a style with a common style as a parent:
<style name="NewAlertButtonStyle" parent="ThemeOverlay.AlertDialog">
<item name="buttonStyle">#style/AlertDialogButtonRed</item>
</style>
And then the AlertDialogButtonRed looks like:
<style name="AlertDialogButtonRed" parent="Widget.AppCompat.Button">
<item name="android:textColor">#color/red</item>
</style>
And this is being set when creating a dialog:
AlertDialog.Builder(ContextThemeWrapper(context, R.style.NewAlertButtonStyle))
.setTitle(getString(R.string.dialog_title))
.setMessage(getString(R.dialog_body))
.setPositiveButton(R.string.ok) { _, _ ->
//doing something here
}.show()
Howevere the style seems not being applied, the button text color is not red. What am I missing?
Just set R.style.NewAlertButtonStyle in ContextThemeWrapper
You are speaking about the control buttons of the dialog itself, right?
They are using android:buttonBarButtonStyle, which is the correct style for you to apply in the dialog overlay.
But you may probably already have changed the usage of your AlertDialog material dialogs of some sort. In that case there might be a different style you need to apply (there I'm not sure).
I stuck for more than 2 full days, doing this.
So far, I can change only the Title text color using style XML file. Other than that no attributes work well. Almost covered all STACK OVERFLOW questions, but not luck, not able to find answers.
Here's what I want to do:
(Delete button is focussed)
As of now, I am able to do this only by programmatically by getting titleDivider, Positive and Negative button and assigning background. But that said I hate to do this by programmatically.
Some one please help me.
Your can use this project AlertDialogPro.
Include this project to yours and define your theme like below:
<style name="YourAppTheme.AlertDialogProTheme" parent="AlertDialogProTheme.Holo.Light">
<!-- Custom the title style -->
<item name="android:windowTitleStyle">#style/YourDialogWindowTitle</item>
<!-- Change the title divider to green -->
<item name="adpTitleDividerBackground">#color/your_green_color</item>
<!-- Change the button style -->
<item name="adpButtonBarButtonStyle">#style/YourButtonStyle</item>
</style>
<style name="YourDialogWindowTitle" parent="DialogWindowTitle.Holo.Light">
<item name="android:textColor">#color/your_green_color</item>
</style>
<style name="YourButtonStyle" parent="Widget.Holo.Button.Light">
<item name="android:background">#drawable/your_button_background_selector</item>
<item name="android:textColor">#color/your_button_text_selector</item>
</style>
And specify this theme to your app's theme with attribute "alertDialogProTheme":
<style name="AppTheme" parent="AppBaseTheme">
...
<item name="alertDialogProTheme">#style/YourAppTheme.AlertDialogProTheme</item>
</style>
Use AlertDialogPro.Builder to build dialog:
AlertDialogPro.Builder builder = new AlertDialogPro.Builder(getContext());
builder.setTitle("Delete").
setMessage("Are you sure?").
setPositiveButton("Delete", null).
setNegativeButton("Cancel", null).
show();
I am customizing my android app by defining theme in styles.xml. I would like to apply a basic text color to all text of my app, but I want to keep the appearance of AlertDialog as default (Holo.Light).
styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:textColor">#android:color/holo_red_dark</item>
<item name="android:alertDialogTheme">#style/AlertDialogStyle</item>
</style>
<style name="AlertDialogStyle" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#style/DialogWindowTitle</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#style/DialogWindowTitleAppearance</item>
</style>
<style name="DialogWindowTitleAppearance" parent="#android:style/TextAppearance.Holo.DialogWindowTitle">
<item name="android:textColor">#android:color/holo_purple</item>
</style>
</resources>
However, the color defined in AppTheme overrides the purple color:
If I remove the line <item name="android:textColor">#android:color/holo_red_dark</item> in AppTheme, the text color of the title changes correctly.
So my question is: How can I define a text color for my app theme, while setting another color (or preserving system default color) to the dialog title text?
define
<item name="android:textAppearanceLarge">...</item>
in your AlertDialogStyle
in Your AndroidManifest.xml set another style to your Activity
<activity
android:name="com.my.sample.Activity"
android:theme="#android:style/Theme.Holo.Light.Dialog"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/my_activity_name">
</activity>
Great work with drilling down to the specific text you want to change. The fact it isn't working looks like a bug to me. If you want a built-in alert dialog window instead of a custom themed one, you can use this function when you build an alert box:
AlertDialog.Builder(Context context, int theme)
"theme" can be AlertDialog.ThemeTraditional or some other AlertDialog theme constant. I imagine you've moved on by now, maybe making your own alert dialog activity to replace it, but the above was an adequate workaround for me (I had white text on light grey background, so I had to do something).
Im using Theme.Holo.Light.DarkActionBar but I dont really like the Light Dialogs/AlertDialogs.
I want to change every Dialog to the dark Holo Dialog.
<style name="CustomDialogTheme" parent="#android:style/Theme.Holo.Dialog">
<item name="#android:background">#9933CC</item>
<item name="#android:textColor">#02FA07</item>
</style>
<style name="Holo.Light.DarkActionBar" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:dialogTheme">#style/CustomDialogTheme</item>
<item name="android:alertDialogStyle">#style/CustomDialogTheme</item>
</style>
Thats what I tried but it has no effect on any Dialogs.
Ok nvm Im stupid its android:alertDialogTheme instead of android:alertDialogStyle. But this messes up the Preference Dialogs, so I just keep using the Light Dialogs.
In AndroidManifest.xml under the application tag, add this line :
android:theme="Holo.Light.DarkActionBar"
The issue is that the styles you're changing android:background and android:textColor are not dialog attributes.
You can see a full list here under Theme.Holo.Dialog on line 1617.
The ones you have to change are probably android:windowBackground and textAppearance
How do I change title's background in this dialog ["Lesson Editor"]?
As you can see,the theme of my application is dark and in the title's background in the main activities are dark as i want, but somehow the dialog isn't.
Help me please!!
Here is the screenshot of my dialog:
Recently I used it for my implementation, so here is my code.
You can do that by creating a new style in the styles.xml file:
<style name="question_dialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowTitleStyle">#style/question_dialog_title</item>
</style>
<style name="question_dialog_title" parent="android:Widget.TextView">
<item name="android:background">#5cc5cc</item>
<item name="android:textSize">21sp</item>
<item name="android:textColor">#ffffff</item>
</style>
and then, when you create your Dialog object, you wanna use this constructor:
answeringDialog = new Dialog(this, R.style.question_dialog);
the output is: