since last night I'm expecting this weird behavior of my application theme:
Popup menus appear with black background and AlertDialogs with white text color.
By the way, heres my styles.xml:
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/action_bar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/action_bar_background</item>
</style>
</resources>
Any idea whats wrong? I just want the popup menu to use the default light theme again and the alert dialog to use a black foreground color.
Why do you think it is wrong?
PopupWindow background is dark in Theme.Light:
<style name="Widget.PopupWindow">
<item name="android:popupBackground">#android:drawable/editbox_dropdown_background_dark</item>
<item name="android:popupAnimationStyle">#android:style/Animation.PopupWindow</item>
</style>
If you want to change it - you need to override popup window style for your theme:
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/MyActionBar</item>
<item name="android:popupWindowStyle">#style/YourCustomStyle</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="YourCustomStyle" parent="#android:style/Widget.PopupWindow">
<item name="android:popupBackground">#drawable/your_background</item>
</style>
Alert dialog gets customized in the same way
OK I solved the problem. I passed the application context to both popup menu and alertdialog builder's constructor. This seems to cause problems with application themes. I simply changed getApplicationContext() to getActivity() and it worked.
Related
I'm building an application with React Native, and with very little knowledge of Android. Mostly this is not a problem, but I would like to use native components for alerts. React native provides a way to launch a native alert, but there is no way to style it from React code. So I have resorted to suggestions online about using the /res/values/styles.xml configuration.
It's possible that it's just my lack of knowledge in Android showing here, but I can't seem to get a good solid reference of XML styling attributes, where they go, how they're structured etc.
My problem currently is that my buttons are showing up with background colours, which I don't want. I want to have standard material design text buttons like you would expect on Android. In the image below, "OK" should show up with green text, and no background.
Here is my styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:textColor">#ffffff</item>
<item name="android:statusBarColor">#16192E</item>
<item name="colorPrimaryDark">#6fca3a</item>
<item name="colorPrimary">#6fca3a</item>
<item name="colorOnSurface">#ffffff</item>
<item name="android:colorBackground">#16192E</item>
<item name="android:colorBackgroundFloating">#16192E</item>
<item name="alertDialogTheme">#style/AlertDialogTheme</item>
</style>
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<item name="buttonBarNeutralButtonStyle">#style/NeutralButtonStyle</item>
</style>
<style name="NeutralButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#f00</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#f00</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#00f</item>
</style>
</resources>
And if anyone can point me in the direction for figuring this stuff out myself that would also be great.
EDIT
This is not a question about how to change the style of a button that I have rendered onto the page in code. This question is specifically about how to style the buttons on a built in native android alert dialog. There does not appear to be a way to do it through JavaScript/React, and the fact that I am able to style the background, text colour, and button colour of the alert lead me to believe that it can be done through the styles.xml file, without having to actually write Android code to extend the Alert Dialog.
In native Android using a Material Components theme you can customize globally the alert dialog using:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
<item name="materialAlertDialogTheme">#style/AlertDialogTheme</item>
</style>
with:
<!-- Alert Dialog -->
<style name="AlertDialogTheme" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
<item name="buttonBarNeutralButtonStyle">....</item>
</style>
Currently I am working on a little android app. I try to change the background color of the ActionBar's dropdown menu. So far I was only able to change the background color of the ActionBar itself, but not of the dropdown menu. It simply does not change the color. Here is some code from my styles.xml:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">#style/MyActionBarStyle</item>
<item name="actionBarStyle">#style/MyActionBarStyle</item>
<item name="android:actionDropDownStyle">#style/MyActionDropDownStyle</item>
</style>
<style name="MyActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/background_color_action_bar</item>
<item name="background">#color/background_color_action_bar</item>
</style>
<style name="MyActionDropDownStyle" parent="#style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
<item name="background">#color/background_color_action_bar</item>
<item name="android:background">#color/background_color_action_bar</item>
<item name="android:popupBackground">#color/background_color_action_bar</item>
<item name="android:dropDownSelector">#color/background_color_action_bar</item>
</style>
The MyActionBarStyle part works but the MyActionDropDownStyle does not. I suppose that the parent of MyActionDropDownStyle might be wrong. Maybe someone of you has an idea and can help me.
Thanks in advance!
I have a lot of overdraw in my app, which caused me to disable the window background completely. After upgrading to appcompat v21 the decorview has a solid color background again (its primaryColor). I tried the following to disable it:
<style name="AppBaseTheme" parent="Theme.Guidantswhite">
<item name="android:windowBackground">#null</item>
</style>
or in my base theme:
<style name="Theme.Guidantswhite" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<!-- Main theme colors -->
<!-- your app's branding color (for the app bar) -->
<item name="colorPrimary">#color/bgo_highlight</item>
</style>
Would be grateful for any pointers.
In my code I sent background for decorview for reason to set it for status bar. Maybe you have the same functionality?
I'm using HoloEverywhere and ActionbarSherlock to create my application. I've been successful in theming the listview i have in my main activity so that the color is grey instead of holo blue whenever pressed or focused. I would like to theme the preference options I have in a PreferenceActivity as well, but it only works for devices with pre-ICS. With devices running android 4.0+, whenever the option is pressed or focused, the color is still ICS blue.
Another question: I'm using a theme that's parent is Holo.Theme.Light.DarkActionBar. How would I make it so that it uses a light overflow popupmenu instead of the dark one? I've tried adding popupmenustyle to my styles, but I've had no luck
styles.xml:
<style name="Theme" parent="Holo.Theme.Light.DarkActionBar">
<item name="android:actionBarItemBackground">#drawable/selectable_background</item>
<item name="actionBarItemBackground">#drawable/selectable_background</item>
<item name="android:activatedBackgroundIndicator">#drawable/activated_background_holo_light</item>
<item name="activatedBackgroundIndicator">#drawable/activated_background_holo_light</item>
<item name="android:listChoiceBackgroundIndicator">#drawable/list_selector_holo_light</item>
<item name="android:actionBarStyle">#style/ActionBar</item>
<item name="actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#style/Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="android:background">#666666</item>
<item name="android:icon">#drawable/icon</item>
<item name="background">#666666</item>
<item name="icon">#drawable/icon</item>
</style>
For me this works:
<item name="popupMenuStyle">#style/PopupMenu.Intellistats</item>
<item name="android:popupMenuStyle">#style/PopupMenu.Intellistats</item>
where:
<style name="PopupMenu.Intellistats" parent="#style/Holo.ListPopupWindow">
<item name="android:popupBackground">#color/holo_orange_dark</item>
</style>
Is there a way I can change all the alert dialogs appearing in my Android application? I want to change the dialogs that are system generated as well (like the Edit Text dialog that opens up when you long tap on any EditText). I want to change the title font color and size of all the dialogs in my app.
Is there a way to do it?
I have tried setting android:alertDialogTheme in my theme. But it seems to be not working. Code following -
<style name="Theme.DLight" parent="android:Theme.Light.NoTitleBar">
<item name="android:alertDialogTheme">#style/DialogStyle</item>
</style>
and
<style name="DialogStyle" parent="android:Theme" >
<item name="android:windowBackground">#drawable/background_holo_light</item>
<item name="android:textColor">#014076</item>
</style>
EDIT
I'm not invoking a dialog from my code. It's just the default dialog
that appears when you long click on any EditText. Generally it
contains the keyboard options like Select word, Select all, Input
method etc.
In you dialog theme, the attribute you need to modify windowTitleStyle. There, reference a style for your title and define a text appearance for this title. For example, to display your title red and bold:
<style name="AppTheme" parent="#android:style/Theme.Holo">
...
<item name="alertDialogTheme">#style/AppTheme.AlertDialog</item>
</style>
<style name="DialogStyle" parent="#style/Theme.Holo.Dialog.Alert">
...
<item name="android:windowTitleStyle">#style/DialogWindowTitle_Custom</item>
</style>
<style name="DialogWindowTitle_Custom" parent="#style/DialogWindowTitle_Holo">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#style/TextAppearance_DialogWindowTitle</item>
</style>
<style name="TextAppearance_DialogWindowTitle" parent="#android:style/TextAppearance.Holo.DialogWindowTitle">
<item name="android:textColor">#android:color/holo_red_light</item>
<item name="android:textStyle">bold</item>
</style>
If you want to style a little bit more your AlertDialog, I wrote a blog post detailing the steps.
In styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
//All your other styles and add the one mntioned below.
<item name="alertDialogTheme">#style/AlertDialogStyle</item>
</style>
In the styles of AlertDialog add:
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:editTextColor">#color/primary_text</item>
<item name="android:background">#color/white</item>
<item name="android:windowTitleStyle">#style/TextAppearance.AppCompat.Title</item>
</style>
windowTitleStyle is important to add as it will give your title that effect that you need.
Now simply use this Theme in your AlertDialog in any activity that you want like below:
AlertDialog.Builder dialog = new AlertDialog.Builder(this, R.style.AlertDialogStyle);