Custom theme for AlertDialog not being applied - android

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).

Related

The night/theme.xml in android studio

The "colorprimary" changes in themes.xml then the button colour also changes "(image in lite mode)enter image description here (image in dark mode)enter image description hereedittext hint selection colour also changes . I want to edit text colour with out changing the theme and without changing button colour
I think, is important to mention first that a theme is different than a style. Basically, when you apply a style it only affects to the component( edit text, view, textview, etc). But when you apply a theme it affects in general terms to the app. More info about in this Link.
So, if you are applying a theme probably you are modifying some attributes that are being used in the app scope such as primaryColor, secondaryColor, etc. Probably, the editText is using primaryColor in order to set the color of the hint and when you change to another theme, primaryColor is affected, then the color hint of the editText is affected as well.
Based on the above description, we can apply multiple solutions. I would like to suggest the following.
default theme.xml
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/gray_medium_dark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorPrimaryVariant">#color/gray_dark</item>
<item name="colorSecondary">#color/cyan</item>
<item name="editTextStyle">#style/CustomEditTextStyle</item>
</style>
nigh theme theme.xml
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowLightNavigationBar" tools:ignore="NewApi">false</item>
</style>
style.xml
<style name="CustomEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="android:textSize">#dimen/text_size_16sp</item>
<item name="android:textColorHint">#color/gray_normal_light</item>
<item name="android:fontFamily">#font/roboto_condensed</item>
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:inputType">textVisiblePassword</item>
</style>
Explanation: Another important point about style and themes is that we can apply something similar to inheritance. It means that we ca re-use some attributes or/and modify others.
So, <item name="editTextStyle">#style/CustomEditTextStyle</item> means that for all the edittext on my application I want to set up CustomEditTextStyle style. Also, in nigh theme we are applying parent as parent="AppTheme.Base" and this parent also has CustomEditTextStyle as style for edittext, so it will no change.

Custom BiometricPrompt.Builder

Can we modify/custom ourBiometricPrompt?
For example right now i use smth like this:
BiometricPrompt.Builder(AppResources.appContext)
.setTitle("title")
.setSubtitle("subTitle")
.setDescription("description")
.setNegativeButton("Cancel", AppResources.appContext?.mainExecutor,
DialogInterface.OnClickListener { dialogInterface, i -> biometricCallback.onAuthenticationCancelled() })
.build()
.authenticate(CancellationSignal(), AppResources.appContext?.mainExecutor,
BiometricCallbackV28(biometricCallback))
Do I have the ability to change the style of the text, title, negativeButton color?
Update2: beta01
The change with the alpha version was that the fingerprint dialog uses android.app.AlertDialog and the new beta01 uses androidx.appcompat.app.AlertDialog which has a private style.
Then to override that style we have to override androidx.appcompat.R.attr.alertDialogTheme reference in our styles.xml, we can do it replacing <item name="android:alertDialogTheme">#style/CustomAlertTheme</item> by <item name="alertDialogTheme">#style/CustomAlertTheme</item> in my case thay I only wanted to change the button text color, I just added <item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item> and the same for posivtive, because replacing alertDialogTheme changes other things that I did not want to.
My styles.xml now:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColor">#color/black</item>
<item name="android:buttonStyle">#style/WibleButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/dark_blue</item>
</style>
Update: beta01
This trick is not working anymore. I'll try if I can find another way to do it.
I found two ways to do it, none of these are specific for the BiometricPrompt, one is to override the theme button style:
<item name="android:buttonStyle">#style/CustomButtonStyle</item>
another option is to override your alert dialog theme:
AppTheme style
<item name="android:alertDialogTheme">#style/AlertDialogTheme</item>
<style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
<item name="android:buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textSize">10sp</item>
<item name="android:textColor">#color/primary_text</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/primary_text</item>
</style>
And then you can apply a specific theme to a specific activity. Maybe they could open the API to allow styles, but I had to change the color because it was impossible to see the buttons (everything was white) and this is what I found to solve my problem. I hope it helps you.
You can't set properties on the prompt that aren't exposed through its Builder - the UI is provided by the system, and is designed to be uniform throughout all apps.
This is sort of the main point of this API, this way the user becomes familiar with the prompt and knows that whatever they're interacting with is safe to use.
Can we modify/custom our BiometricPrompt?
The BiometricPrompt uses the "buttonStyle" style that is set for your theme, which you can adjust in the styles.xml
Keep in mind that this is the default button style, so if you do not want other buttons to change you would have to assign buttons etc. their own style. (and dialogs etc.)
For example, if you only want the text color to be different for the BiometricPrompt, you could do something like this:
<style name="myTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="buttonStyle">#style/ButtonPrimaryStyle</item>
</style>
<style name="ButtonPrimaryStyle" parent="ButtonParentStyle">
<item name="android:textColor">454545</item>
</style>
<style name="ButtonPrimaryStyleMain" parent="ButtonPrimaryStyle">
<item name="android:textColor">e2e2e2</item>
</style>
This would make the BiometricPrompt have a dark grey text for the cancel button, while every button with the "ButtonPrimaryStyleMain" style will have a lightgrey, almost white, text color.
You would also have to assign this theme to your application in the manifest.
<application
android:theme="#style/myTheme"

Overriding the theme attribute inside of a style?

I have a project where I defined all the styles in the theme (Button style, Checkbox Style, EditText style and so on) This way I don't need to apply any style or theme in the layouts where I use those views, because they are applied automatically by my AppTheme.
Now I encountered a problem. I wanted to define the Switch style inside the theme but it should use another color for the colorControlActivated and colorControlHighlight. By default it uses the colorPrimary which I defined in the theme, but what if I want to change that.
The problem can be fixed easy with a theme overlay or a style where I override the needed attributes that I mentioned above and apply that style/theme everywhere where I use the Swtch view. But I want to know if I can avoid that and define a default style for my Switch views inside the same theme where the colorControlActivated and colorControlHighlight are already defined.
I tried several things but this looked like the one that actually might work but it does not:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorPrimary</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
<item name="colorSwitchThumbNormal">#color/white</item>
<item name="switchStyle">#style/SwitchStyle</item>
</style>
<style name="AppTheme.GreenControlOverlay">
<item name="colorControlActivated">#color/green</item>
<item name="colorControlHighlight">#color/green</item>
</style>
the SwitchStyle looks like this
<style name="SwitchStyle" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="android:theme">#style/AppTheme.GreenControlOverlay</item>
</style>
I dont know why this is not working because if I set the android:theme inside my AppTheme directly it does override the colorControl attributes, but if you override it from a style it does not work. If I apply this GreenControlOverlay on the Switch view inside of my layout it also works.
Is it even possible to do this?
If you have any questions please don't hesitate to ask. I hope I explained my problem well.

Android EditTextPreference dialog style/theme

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.

Android theme text color conflicts with alert dialog title color

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).

Categories

Resources