Define multiple textColor in one theme - java

Currently I'm trying to define in my application a theme for day mode and another for night mode.
I can already change between those two, the problem is that I have textViews with white text color and others with black text color. So I would like to know if there is a way for:
Define multiple textColors in one theme;
OR
Define themes that change the style, e.g.
// defined in the layout
<TextView style="#style/whiteStyleText" />
<TextView style="#style/blackStyleText" />
// defined in the styles
<style name="Theme.DayTheme" parent="#style/Theme.ActivityTheme">
<item name="#style/whiteStyleText">#color/white</item>
<item name="#style/blackStyleText">#color/black</item>
</style>
<style name="Theme.NightTheme" parent="#style/Theme.ActivityTheme">
<item name="#style/whiteStyleText">#color/light_red</item>
<item name="#style/blackStyleText">#color/red</item>
</style>
Many thanks in advance.

this might help you a bit. Not a 100% sure what you want.
It is to define all your colors in one place. Place this is res>values>styles.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<color name="blueDefault">#2782AF</color>
<color name="blueBGDark">#226A87</color>
<color name="blueDark">#257493</color>
<color name="blueLight">#297F99</color>
<color name="greyDefault">#3F4144</color>
</resources>

I haven't found anything about multiple textColors defined in the same theme.
One solution, which I've done is, set a default textColor for the dayTheme and another one for the nightTheme.
For all other textViews that does not use that default color, I've defined it in the layout, and at the beginning of the activity I check what is selected, day or night mode and I call setContentView with the proper layout.
Another solution would be, declare a custom attribute, e.g. textColorNight, and create a custom textView which would use this attribute and then would check what mode is currently selected, so it could decide if the textColor (day mode) or the textColorNight (night mode) would be applied to it's text.
I thing this second solution is a more refined one, but I haven't implemented it because of the lack of time. But next time I need to implement the day/night mode, I'll do it this way.

Related

How do I make the android home button bar transparent? [duplicate]

This question already has answers here:
completely transparent status bar and navigation bar on lollipop
(12 answers)
Closed 5 years ago.
My app has a black bar over these home buttons and i need them to look transparent like below
set style to your main theme like below code
<resources>
<style name="Theme" parent="android:Theme.Material.Wallpaper.NoTitleBar">
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:navigationBarColor">#android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>
To do that inside of an App I used this into styles.xml
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>
set navigationBarColor to change color.
Check here to see flags required to mention. If color is opaque then there is different set of flags else another set of flags.
void setNavigationBarColor (int color)
Blockquote
Sets the color of the navigation bar to . For this to take effect, the
window must be drawing the system bar backgrounds with
FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS and FLAG_TRANSLUCENT_NAVIGATION must
not be set. If is not opaque, consider setting
SYSTEM_UI_FLAG_LAYOUT_STABLE and
SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION.
The transitionName for the view background will be
"android:navigation:background".

How would I dynamically change the font color for a whole android app?

I have my styles.xml file for the app:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColor">#000000</item>
<item name="android:background">#FFFFFF</item>
</style>
I want to make it so that if a user selects a value from a spinner then that value changes the textColor in the styles app.
I have my onClick method and I have the value from the spinner but I can't figure out how I access the textColor styles item and how I dynamically change it?
I didn't manage to do exactly what I wanted but a workaround was that I only let the user choose between 3 font colours: black, blue, and red, and then I had 3 themes. Each theme had the different text colour.
Then I just switched the theme following this answer: Android - Change app Theme on onClick
One way to achieve this is to make a color.xml resource in the rescourses folder and link to it in the activity layout.
Also why do you have css as one of the tags for this question?
Rather it should be XML.

Change Theme Color at Runtime in Android [duplicate]

This question already has answers here:
Set theme color dynamically
(5 answers)
Closed 4 years ago.
Theme colors can be changed if multiple themes added in app and can be switched at run time easily. Other way is to change the color of actiobar, textview etc one by one of all the object. This way is very long and take lots of code. Simple way i found is to change colors declare in colors.xml file but i could not find a way programmatically or third party library to do that. Is there a way to do this.
My Colors XML File:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#ea1e63</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
I just want to change colorPrimary color at runtime, this will change color on all app activities. I found https://github.com/negusoft/GreenMatter, to change colors at runtime but its old and dont work with android studio.
when you apply theme and want to change color than
EX :
activity.setTheme(R.style.green);
<style name="green">
<item name="main_background">#drawable/background_green</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="button_light">#color/button_light</item>
</style>
theme color can not be change at runtime of a particular theme.
Make custom style and change these styles when you want to change color in your application.

Editing XML Style Sheets Changes Buttons in Android

I'm working on an Android app and decided I wanted to change the colors across the whole app, so I used an xml style sheet. This changed my colors, but it didn't change my EditText, TextView, and Button colors, so I had to make an individual style sheet for each element and then call those sheets from within the main color sheet I was using.
However, once I did that, the colors acted appropriately for the three widgets, but the boxes shrunk and disrupted our entire layout. We believe it's because we had overridden the default styles for those, but I thought the way it was written, we were merely overriding the color options.
I'd like to figure out how to change the colors of EditText, TextView, and Button, but not change their formatting.
The Styles.xml for our project:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="oldblue">#33b5e5</color>
<color name="gungrey">#9C9C9C</color>
<color name="textGold">#ffe27e</color>
<color name="backgroundBlack">#000000</color>
<color name="buttonGrey">#222222</color>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</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:textColor">#color/textGold</item>
<item name="android:windowBackground">#color/backgroundBlack</item>
<item name="android:colorBackground">#color/backgroundBlack</item>
<item name="android:editTextStyle">#style/TextEditStyle</item>
<item name="android:textViewStyle">#style/TextViewStyle</item>
<item name="android:buttonStyle">#style/OurButtonStyle</item>
</style>
<!-- Allows us to edit the EditText field -->
<style name="TextEditStyle" parent="android:Widget.EditText">
<item name="android:background">#e3e3e3</item>
</style>
<!-- Allows us to edit the TextView words (much of the app text) -->
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#color/textGold</item>
</style>
<!-- Allows us to edit the Buttons in the app -->
<style name="OurButtonStyle" parent="android:Widget.Button">
<item name="android:textColor">#color/textGold</item>
<item name="android:background">#color/buttonGrey</item>
<item name="android:padding">10dp</item>
<!-- This line here helped us fix the issue with the buttons colliding with the textboxes, but ideally, we want to change a single thing and fix all the widgets and not have to use padding here -->
</style>
</resources>
Our app is using Linear Layout on all of our pages. We would like to keep it this way if possible.
Please let me know if you need to see any screenshots or xml code of the individual pages. They're all having the same error, though.
Thanks for any help you can give!
Instead of android:, try prefacing your parent references with #android:style/.
When you're modifying the background attribute for aButton or EditText, you're replacing the entire background that already exists which is why you're losing all the "formatting".
So with a Button, the default buttons you see are actual images.
For example, btn_default_normal_holo.9.png located in (path-to-sdk)\platforms\android-19\data\res\drawable-xhdpi
One way is to create 9 patch images to create your background. You can refer to the answer by Basim Sherif here. In that question, it asks a similar question to yours. So in this case you can use the existing images and modify the colors.
Otherwise, you will need to set your own styles (drawables and selectors) to "match" the original formatting or using your own formatting.
Also note that since you set the background of your buttons to a color, there aren't any selectors anymore (like changing the color when the button is selected).

Changing actionBar dropdown background color

How do i change the color of the background where it says "add contact" and "about". Now its kind of grayish, but i want it to be white! This is a actionBar with dropdown, not a spinner. And im not using that actionbarsherlock thing.
For example, in you exisiting parent Style definition, add just the android:popupMenuStyle attribute as shown below:
<style name="Theme.Example" parent="#android:style/Theme.Holo.Light">
<item name="android:popupMenuStyle">#style/PopupMenu.Example</item>
.......
.......
</style>
And override the android:popupMenuStyle attribute by defining your style:
<style name="PopupMenu.Example" parent="#android:style/Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_example</item>
</style>
The #drawable/menu_dropdown_panel_example is this:
You can either choose to use an image similar to the one above, or use an Color resource.
And this is for the default ActionBar. Not the ActionBarSherlock. ;-)
ActionBar styles can be easily created using this cool website: http://jgilfelt.github.com/android-actionbarstylegenerator/. I usually experiment a little here before deciding on the style. Plus, it lets you download the styles and all necessary resources. Just plug them in your app and you are good to go. :-)
UPDATED: I think I caused some confusion which has resulted in the loss of the shadow on your app's drop-down. You may have replaced all the <items> in your Style that I listed in the original suggestion. The edit should fix that.
UPDATE 2:
Use these image resources instead of the #android:color/white value you are currently using. These are in the order of XHDPI, HDPI and MDPI. Save them and use them in the Style definition.
Try to get styles from ActionBarStyleGenerator service. It has preview and makes all necessary drawable, xml and styles.
There you can easily find and change required color and then find it in sources (in your case it is android:popupBackground).
You can simple override itemBackground item in your theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:itemBackground">#color/skyBlue</item>
</style>

Categories

Resources