Changing color themes in Android Studio - android

I am trying to change the color of my Hello World app.
Right now it looks like this in the emulator (purple bar, pink floating icon):
But when I change the theme setting, I get a bunch of rendering errors (note that I tried to choose Material Light):
And when I try to run the app again it still looks purple/pink as it did in the first pic. I'm not really sure how to make heads or tails of these kinds of errors or how I am supposed to fix them.

You are changing the theme of the preview window. This is used to view how your layout would look using a given theme. It does not change the theming on your phone when running your app—it is just a preview.
To actually change the colors you need to change the theme. Usually this would be the AppTheme that is referencing some colors.
Locate your colors.xml in res/values/ and modify the colors there:
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
These colors get used by the theme generated at project creation, looking like this in styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
You could also remove the reference and edit the colors in the theme directly, but that is usually not the clean way. Your styles.xml would also be the right place to add more themes—which in turn you could preview in Android Studio.

Related

Multiple themes changing tab icon color

I have been learning MDC from google code lab, and it is a well designed application to know how material design works. However, in a particular section they showed how to use dark theme. The did it by changing the theme attributes from style.xml. Being resourceful, I have been trying to learn how to use multiple theme and interchange them runtime.
For this reason I avoided their hard-coded way and tried to inherit the base theme and put changes according to my need. Below I am putting some changes in the theme file
<!--Dark Theme style !-->
<style name="Theme.Shrine.Dark" parent="Theme.Shrine">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/darkColorPrimaryDark</item>
<item name="colorPrimaryDark">#color/darkColorPrimaryDark</item>
<item name="colorAccent">#color/darkColorAccent</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
<item name="android:textColorPrimary">#color/darkTextColorPrimary</item>
<item name="colorControlActivated">#color/darkColorControlActivated</item>
<item name="toolBarStyle">#style/Widget.Shrine.Toolbar.Dark</item>
<item name="appBackGroundColor">#color/darkBackgroundColor</item>
</style>
There are some changes but they are irreverent to my problem, so I am not going to add them here. Anyway, with changes in my style file app looks something like this
Everything is expected except for icon colour in toolbar. So I check into the code and find every icon colour is referenced from the their respective from drawable file with android:tint="#color/toolbarIconColor" and in color.xml toolbarcolor is <color name="toolbarIconColor">#color/textColorPrimary</color>
It shows wrong color in dark mode, in this case how can I show yellow color in dark mode. I have changed textColoeSecondary from base theme of dark theme but it didn't work
Firstly, you need to remove hard reference of colour from every drawables which reflect different colours depending on app theme. So, add attrs.xml in your values directory. and add the reference name for icon colour such as this one
<attr name="toolbarIconColor" format="reference"/>
Secondly, add two different colour for two themes. For example, for normal theme <color name="toolbarIconColor">#color/textColorPrimary</color> and for dark one use <color name="darkToolbarIconColor">#FFCF44</color>
Finally, go to your style.xml file and make this change to hook up with the reference we have added in attrs.xml file, like this one for normal theme <item name="toolbarIconColor">#color/toolbarIconColor</item> and <item name="toolbarIconColor">#color/darkToolbarIconColor</item>

Android actionbar color doesnt change

I have the following styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:colorAccent">#color/accent</item>
</style>
</resources>
Where primary is defined as:
<color name="primary">#9c27b0</color>
However in the preview the actionbar is still black and not deep purple.
When i open the theme editor the actionbar is also deep purple.
Is this a bug in the preview? Or have i done something wrong?
In general how exactly do i use the styles.xml? Can i also define button's in it?
After editing the color code in the color.xml file, you should rebuild it. Try rewriting the code and then rebuilding. I faced the same problem, that's ho

how to change android app's look at runtime

I have defined colors in colors.xml like below
<resources>
<color name="primary">#5D4037</color>
<color name="primary_dark">#4E342E</color>
<color name="accent">#FF3D00</color>
</resources>
I have also defined themes in styles.xml, like below
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
Also, at many places in code, I have used these colors to set it for text, etc.
Now I want to give option to users to change the colors. I read that I can't change colors specified in colors.xml. Also, even if I change the theme, the color referenced can't be changed. So specifying more themes (with hard-coded colors) could be an option, but in that case what will happen to elements which has got color dynamically? Also, user cannot select any color but has to use only the colors specified by me in themes.
Am I missing something obvious? How this can be done?
You cannot change values in your resource files at runtime.
If you want to allow your users to customize the colors in your app, you will need to store an manage those colors yourself, perhaps using resource files to provide defaults or to store the options that users can select from.

Android add custom attribute to styles

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.

Changing Android App Background Including Spinners and Dialogs

I want to change default white background in my android app to another color.
I am using android:background="#HEXCODE" in my activity, it changes the background but the dialogs and spinners still have white background.
I also tried creating styles / custom theme but it changes the entire theme (the button and spinner style look like Androind 2.3 instead of ICS look and feel)
I don't want to change anything but on the background that is consistent also in spinners and dialogs.
Thanks.
EDIT
Style Code:
<style name="MyTheme" parent="android:Theme.Light">
<item name="android:background">#FFFFBB</item>
</style>
Manifest:
android:theme="#style/MyTheme"
Try setting the color of the backgrounds of all widgets and the layout by
android:background="#color/black">
in your values folder make a xml like this
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
<color name="black">#000000</color>
</resources>
This worked for me.

Categories

Resources