I want to change color of the blue parts in the following image:
I know how to use custom styles, I just need to know what are called the properties of these blue parts (I mean something like <item name="android:background">#FF0000</item> ) thx
What I really want to achieve is demonstrated here:
You may inherit the theme used from Android code and change particulars within that.
http://developer.android.com/guide/topics/ui/themes.html
<style name="GreenText" parent="#android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
parent holds the value of the default theme. In your case it will be the Dialog theme for Holo.
Related
My app theme is set up like this:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorOnSurface">#color/appColorOnSurface</item>
...
</style>
But when I use MaterialAlertDialogBuilder the text contrast is very poor (because material dialog uses colorOnSurface with 60% alpha, instead of textColorPrimary). So I tried to use this ThemeOverlay:
<style name="ThemeOverlay.App.Dialog.HighContrast" parent="ThemeOverlay.MaterialComponents.Dialog">
<item name="colorOnSurface">#color/appColorOnSurfaceHighContrast</item>
</style>
and applying it like this:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="materialAlertDialogTheme">#style/ThemeOverlay.App.Dialog.HighContrast</item>
<item name="colorOnSurface">#color/appColorOnSurface</item>
...
</style>
However, this causes problems when displaying a list of items in the dialog. Each item touch area is limited to the area of text being displayed instead of stretching the width of the dialog like normal.
Furthermore, the theme does not appear to be Material, but rather AppCompat style.
Why does the ThemeOverlay approach cause the unexpected touch area (as if WRAP_CONTENT) issue? Is that not the correct way to apply a ThemeOverlay? Or is there another way to get the alert dialog to use #color/appColorOnSurfaceHighContrast?
I fixed this by using ThemeOverlay.MaterialComponents.MaterialAlertDialog instead of ThemeOverlay.MaterialComponents.Dialog.Alert, and also using materialAlertDialogBodyTextStyle to make sure only the dialog text body was being styled:
<style name="ThemeOverlay.App.Dialog.HighContrast" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialAlertDialogBodyTextStyle">#style/MaterialAlertDialog.App.Body.Text.HighContrast</item>
</style>
<style name="MaterialAlertDialog.App.Body.Text.HighContrast" parent="#style/MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textColor">#color/appColorOnSurfaceHighContrast</item>
</style>
But why does AndroidStudio auto-complete only show ThemeOverlay.MaterialComponents.Dialog.Alert and not ThemeOverlay.MaterialComponents.MaterialAlertDialog?
Note: there were actually two issues going on here:
Changing the parent ThemeOverlay from ThemeOverlay.MaterialComponents.Dialog to ThemeOverlay.MaterialComponents.Dialog.Alert fixed the touch area issue, but I was still getting an AppCompat (non-Material) theme.
Changing the parent ThemeOverlay from ThemeOverlay.MaterialComponents.Dialog.Alert to ThemeOverlay.MaterialComponents.MaterialAlertDialog fixed the theme to make it appear Material.
UPDATE: It looks like ThemeOverlay.MaterialComponents.Dialog.Alert is for use with alertDialogTheme and ThemeOverlay.MaterialComponents.MaterialAlertDialog is for use with materialAlertDialogTheme. See: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/dialog/res/values/themes.xml#L60
However, that still doesn't explain why the latter doesn't auto-complete.
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.
I'm trying to put a style in all my app, so i created a theme with my style inside :
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
</resources>
But textAppearance doesn't work it stay the same, but when i put something like textColor in my theme, it works
This is a quite old question, but the answer may help someone.
The key to solve this is in the "precedence order of styling techniques" here:
on the top is the highest precedence, at the bottom is the lowest precedence.
As we can see theme has the lowest precedence, in your example, your android:textAppearance property is being overridden by the default style of every view that accepts this attribute, the default style property is defined in every them for every specific view that accepts this attribute, in this case android:Theme.Holo.Light provides the default style for textView as android:textViewStyle... for buttons is android:buttonStyle (which inherits its textAppearance from TextView), and so on.
So if you are trying to apply that android:textAppearance property to a TextVew you should use <item name="android:textViewStyle">#style/subtitle</item> instead of <item name="android:textAppearance">#style/subtitle</item> inside MyTheme. Away to veryfy this is setting android:textViewStyle to null, that way your current code will work fine with textViews <item name="android:textViewStyle">null</item>
This post explains this precedence a bit deeper:
https://medium.com/androiddevelopers/whats-your-text-s-appearance-f3a1729192d
What I can see is, you have not declared the color in your xml for theme. Please add the following line within the <resources> and try. Your xml will look like:
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<color name="purple">code for your color</color>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
I think this will do.
Depends on your target API you need to put your customization code in different /res/values-vxx/style.xml files.
For TextView, try android:textAppearanceSmall inside your theme instead.
I'm trying to implement a feature to let the user change the theme.
I've done this but depending on if the theme is light or dark, I need to use the opposite color to draw line on a canvas. I thought the best way to do this would be to simply call R.style.colorAttribute to get the correct color when doing the drawing.
In the styles.xml I have defined the following themes:
<style name="DarkTheme" parent="android:Theme.Holo" />
<style name="LightTheme" parent="android:Theme.Holo.Light" />
Can anyone help on the best way to add an attribute here which will store the color. I've not done this before and was n't sure if I should use the color.xml file or the styles.xml file.
Thanks
And just to make it clear
I for the dark theme I need a white color
and for the light them I need the same attribute but in black.
If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional light theme to use your own color like this:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
More about styles on developer.android.com.
I hope I can explain what I'm after. In essence, my users have asked me to allow different looks in my application, which I hope I can do with themes.
I hoped I could do something like this:
<style name="NewTheme" parent="android:Theme.Dark">
<item name="labelColor">#f90</item>
<item name="buttonColor">#fff</item>
<item name="buttonBg">#drawable/button</item>
</style>
<style name="OldTheme" parent="android:Theme.Dark">
<item name="labelColor">#fa0</item>
<item name="buttonColor">#88f</item>
<item name="buttonBg">#drawable/button_old</item>
</style>
And then reference these values in my styles.xml:
<style name="labelStyle">
<item name="android:textColor>#labelColor</item>
</style>
<style name="buttonStyle">
<item name="android:textcolor">#buttonColor</item>
<item name="android:background">#buttonBg</item>
</style>
I know this syntax is wrong, but what might be the right syntax? Basically, I want to create sets of attributes (color, background, a couple other things) and select them based on theme.
To work with themes and styles in Android you have to:
Define one or more themes in themes.xml and set the definitions of
your styles there.
Define custom attributes, a.k.a. custom styles, in attrs.xml.
Describe what the values of your custom styles are in styles.xml.
In your layout files, give your views a style attribute, which has a
custom style name as their values.
Set the theme of your application or activity in either
AndroidManifest.xml or in the Activity's onCreate(). This is done by calling setTheme() in the activity's onCreate() method, before any call to setContentView().
To change the theme, you simply need to restart your activity.
Iadvice you to look at this tutorial it deals with all that a programmer want to work on android themes (text color, text formatting, state list drawable etc ...)