The night/theme.xml in android studio - android

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.

Related

Change EditText long click menu text color

For some reason the menu that comes up on long click (for copy/paste/cut/etc...) in my EditText have white text color on a white background so it's unreadable.
I searched quite a lot on this forum but haven't found answer that would have worked so far, I have also tried adding a custom popup style for the base theme as well as for the EditText style with colorAccent, color and textColor changed, like this:
<style name="Base.AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Tried all 3 separately -->
<item name="android:popupMenuStyle">#style/CustomPopup</item>
<item name="popupMenuStyle">#style/CustomPopup</item>
<item name="popupTheme">#style/CustomPopup</item>
</style>
<style name="Style.Input.EditText" parent="Widget.AppCompat.EditText">
<!-- Tried all 3 separately -->
<item name="android:popupMenuStyle">#style/CustomPopup</item>
<item name="popupMenuStyle">#style/CustomPopup</item>
<item name="popupTheme">#style/CustomPopup</item>
</style>
<style name="CustomPopup" parent="Widget.AppCompat.Light.PopupMenu">
<item name="android:colorAccent">#000000</item>
<item name="android:color">#000000</item>
<item name="android:textColor">#000000</item>
</style>
Unfortunately it did not work.
Does anyone know of a way to customise this menu, the text color in particular?
Check the theme style
<item name="android:background">#FFFFFF</item>
It's highly likely that you have it set to WHITE and the text is already being set to WHITE. I would comment out this line where you're setting the background color and see if you can't figure it out from there. This was my issue.

How to apply style to all Buttons but not to ImageButtons?

I need to apply a background color to all the buttons in my application, so I added this to styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- button styles -->
<item name="colorButtonNormal">#color/button_color</item> <!-- below api 21 -->
<item name="android:colorButtonNormal">#color/button_color</item> <!-- above api 21 -->
<item name="android:buttonStyle">#style/ButtonColor</item>
</style>
<style name="ButtonColor" parent="#style/Widget.AppCompat.Button">
<item name="android:textColor">#color/font</item>
</style>
It worked perfectly, when I add a button in XML, the button has automatically the colors I set in styles.xml
The problem is that when I add a ImageButton, the ImageButton is getting also those colors, and I don't want that. The problem is specifically with colorButtonNormal, which applyes the background color to the ImageButton also and I don't want that. How can I solve this problem?
The default style used by the ImageButton (in your case AppCompatImageButton) is defined in the app theme by the attr:
<item name="imageButtonStyle">#style/Widget.AppCompat.ImageButton</item>
In this style the background attribute <item name="background">#drawable/btn_default_material</item> is tinted with the android:tint="?attr/colorButtonNormal".
You can use a custom style starting from btn_default_material for the ImageButton or you can override the color in the layout.
Something like:
<ImageButton
android:theme="#style/MyImageButtonTheme"/>
with something like:
<style name="MyImageButtonTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorButtonNormal">#color/my_color</item>
</style>
Also evaluate to migrate to Material Components and the MaterialButton.
In this case it is very simple to override the color just using the styles:
<style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ButtonStylePrimaryColor</item>
</style>
<style name="ButtonStylePrimaryColor">
<item name="colorPrimary">#color/....</item>
</style>
You can do it by applying different style for defferent views
Let's say i have three style with defferent color combination and i wish to apply bigButtonTheme style for all big button
Then
i add this line on all big button android:theme="#style/bigButtonTheme">
Rest button or other vewis will remain unchanged.
You can also copy other properties of a style like global themes font background color by deffineing as parents theme
<style name="ButtonColor" parent="#style/GlobelStyleOfApp"/>

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 TimePickerDialog styling guide/docs?

I'm trying to style a TimePickerDialog for sdk 21+ (Lollipop). So far I've figured out how to change the default colorscheme in XML:
<style name="TimePickerTheme" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">#ff2d6073</item> <!-- no effect -->
<item name="colorPrimaryDark">#ff2d6073</item> <!-- no effect -->
<item name="colorAccent">#ff2d6073</item>
<item name="android:textColor">#ffD0D102</item>
<item name="android:textColorPrimary">#ffD0D102</item>
</style>
This works but I'm looking for a guide or documentation for all the properties I can change.
AccentColor does the basic color scheme
TextColorPrimary does the text color
But what property, for example, do I need to change the big text in the 'header' of the dialog (where the current selected time is displayed)?
Is there some documentation that lists all the possible things you can change?
After digging through the AOSP theme and style xml files and a lot of googling I made some progress. I am now able to style most(!) things.
So this is a partial answer, not all the way there yet. But here's how far I got:
You can see that I'm now able to theme the header, the un(!)selected time part (minutes in this case), the circle, the numbers in that circle and the 'hand' (or selector). Oh, and the buttons are styled, too.
Let me explain how I got things working, first: the important thing is that you can't override things directly from you app's theme OR from a (alert)dialog theme/style. You have to go from one to the next, so to speak.
Example:
AndroidManifest.xml: Set custom theme for app and/or activity
<activity>
android:theme="#style/Theme.MyTheme"
</activity>
values-v21/styles.xml: (where your custom theme resides): set the timePickerDialogTheme
<style name="Theme.MyTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:timePickerDialogTheme">#style/TimePickerDialogTheme</item>
</style>
Then below that, define the timePickerDialogTheme and set the timePickerStyle:
<style name="TimePickerDialogTheme" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#ff2d6073</item> <!-- colorAccent here seems to work just fine? -->
<item name="android:timePickerStyle">#style/TimePickerDialogStyle</item>
</style>
Now you can define most of the styling here..
<style name="TimePickerDialogStyle" parent="#android:style/Widget.Material.Light.TimePicker">
<item name="colorAccent">#ff2d6073</item> <!-- colorAccent here seems to work just fine? -->
<item name="android:timePickerMode">clock</item>
<item name="android:headerBackground">#ff2d6073</item>
<item name="android:headerTimeTextAppearance">#style/TextAppearance.TimePickerDialogStyle.TimeLabel</item> <!-- TimePicker Time *TextAppearance* -->
<item name="android:numbersTextColor">#ff000000</item>
<item name="android:numbersSelectorColor">#ff2d6073</item>
<item name="android:numbersBackgroundColor">#ffdddddd</item>
</style>
The important line in the above is:
<item name="android:headerTimeTextAppearance">#style/TextAppearance.TimePickerDialogStyle.TimeLabel</item>
Because if you want to style the text (well, time, actually) in the header you need to define the headerTimeTextAppearance:
<style name="TextAppearance.TimePickerDialogStyle.TimeLabel" parent="#android:style/TextAppearance.Material">
<item name="android:textSize">60sp</item> <!-- from -->
<item name="android:textColor">#ffD0D102</item>
</style>
Now, if you take a look at the Widget.Material.TimePicker in AOSP styles.xml (ctrl-f 'timepicker' until you find it) you'll notice a bunch of other properties that you should be able to modify:
headerTimeTextAppearance
headerAmPmTextAppearance
headerSelectedTextColor
headerBackground
numbersTextColor
numbersBackgroundColor
amPmTextColor
amPmBackgroundColor
amPmSelectedBackgroundColor
numbersSelectorColor
Most of these work (as long as you prepend 'android:' for each of them) BUT I could not get 'headerSelectedTextColor' to work. I got a compile error saying something like "could not match property bla bla". Also, if you look at my example above, I hardcoded the textSize for the 'headerTimeTextAppearance' property because the '#dimen/timepicker_ampm_label_size' value threw errors.
In short: most of the things are listed above and how to get them working. But not all is clear. So I'd still see that complete documentation/guide :)
Android TimePicker material style with custom colors below, you can see http://www.zoftino.com/android-timepicker-example for TimePicker usage and styles.
<style name="MyAppThemeFour" parent="Theme.AppCompat.Light">
<item name="android:timePickerDialogTheme">#style/MyTimePickerDialogStyle</item>
</style>
<style name="MyTimePickerDialogStyle" parent="#style/ThemeOverlay.AppCompat.Dialog.Alert">
<item name="showTitle">false</item>
<item name="colorControlActivated">#ffd600</item>
<item name="colorAccent">#b71c1c</item>
<item name="android:textColorPrimary">#43a047</item>
<item name="android:textColorSecondary">#f44336</item>
</style>
When using version 1.5.0 of the Material Design Library for Android, I've found that I can get most of the theming with using this particular style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTimePickerTheme" parent="ThemeOverlay.MaterialComponents.TimePicker">
<item name="android:textColor">#FF121212</item>
<item name="android:textColorPrimary">#FF121212</item>
<item name="android:textColorSecondary">#FFF9F9F9</item>
<item name="colorAccent">#FF121212</item>
<item name="colorControlNormal">#FF121212</item>
<item name="colorPrimary">#FF121212</item>
<item name="colorSurface">#FFF9F9F9</item>
</style>
</resources>
This will yield in a generic - non colored - Dialog which works for white theme. For dark theme, simply invert the colors.
I've also asked here to have dynamic theming supported for this component.
Example screenshot using the above style:

Styling ActionBar dropdown menu

I'm using a custom theme that inherits from DarkActionBar and I want to customize dropdown menu to be white like when using Light Holo theme.
I've been able to change the background to white using:
<style name="MyTheme" parent="#style/Theme.Light.DarkActionBar">
<item name="android:actionDropDownStyle">#style/MyDropDownNav</item>
</style>
<style name="MyDropDownNav">
<item name="android:background">#drawable/spinner_background_white</item>
<item name="android:popupBackground">#drawable/menu_dropdown_panel_whyite</item>
<item name="android:dropDownSelector">#drawable/selectable_background_white</item>
</style>
But I haven't any clue of how to change the text color to black. Because after setting white drawable the problem is that text isn't visible because is white on white background.
I answer myself after some investigation.
In addition to question's styling you need to:
Customize android:spinnerDropDownItemStyle for actionBarWidgetTheme changing it's text appearance.
Also don't forget that dropdown list is managed by the adapter you use. Then if you used the standard one (simple_dropdown_item_1line) there's no problem. But if you used a custom one like me (to be able to add an icon) don't forget to apply style="?attr/spinnerDropDownItemStyle" in your layout TextView.
Then final custom style is:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.myapp" parent="#style/Theme.Light.DarkActionBar">
<item name="android:actionDropDownStyle">#style/myapp_DropDownNav</item>
<item name="android:actionBarWidgetTheme">#style/myapp.actionBarWidgetTheme</item>
</style>
<style name="myapp.actionBarWidgetTheme" parent="#style/Theme.">
<item name="android:spinnerDropDownItemStyle">#style/myapp.Widget.DropDownItem.Spinner</item>
</style>
<style name="myapp_DropDownNav" parent="#style/Widget.Spinner.DropDown.ActionBar">
<item name="background">#drawable/spinner_background_ab_myapp</item>
<item name="android:background">#drawable/spinner_background_ab_myapp</item>
<item name="android:popupBackground">#drawable/menu_dropdown_panel_myapp</item>
<item name="android:dropDownSelector">#drawable/selectable_background_myapp</item>
</style>
<style name="myapp.Widget.DropDownItem.Spinner" parent="Widget.DropDownItem.Spinner">
<item name="android:textAppearance">#style/myapp.TextAppearance.Widget.DropDownItem</item>
</style>
<style name="myapp.TextAppearance.Widget.DropDownItem" parent="TextAppearance.Widget.DropDownItem">
<item name="android:textColor">#color/black</item>
</style>
Where drawables in myapp_DropDownNav are white background ones that you can generate with ActionBar Style generator in Android Asset Studio
Try setting itemTextAppearance. That should achieve what you want.
I have stumbled on what may be the simplest way to do this. I was working with the AppCompat library.
<style name="ApplicationTheme" parent="#style/Theme.AppCompat">
<item name="android:actionBarWidgetTheme">#style/Theme.AppCompat.Light</item>
<item name="actionBarWidgetTheme">#style/Theme.AppCompat.Light</item>
</style>
My advice is to simply inherit from the Sherlock.Light theme and change the applicable fields to the Dark values. For my app, we wanted a white "up" icon, and white text for the action labels. I don't provide dark versions of my actionbar icons, so they are all white anyway. So after several hours messing around with it and following different people's suggestions, I finally found what I was looking for in the ABS themes file.
I inherit from Sherlock.Light (well, technically HoloEverywhereLight.Sherlock but...) and change:
<item name="android:actionMenuTextColor">#color/White</item>
<item name="actionMenuTextColor">#color/White</item>
<item name="android:homeAsUpIndicator">#drawable/abs__ic_ab_back_holo_dark</item>
<item name="homeAsUpIndicator">#drawable/abs__ic_ab_back_holo_dark</item>
<item name="android:dividerVertical">#drawable/abs__list_divider_holo_dark</item>
<item name="dividerVertical">#drawable/abs__list_divider_holo_dark</item>
That's it. It's way simpler and easier than trying to extend classes, restyle things in code, etc.

Categories

Resources