This is the home screen of my app. I have used Drawable and Color resources in the design. Now I want to have multiple color sets (for the parts with cyan color) so user can change them at run time. I searched a lot, but they don't fit my case.
Note that I want just the cyan colored parts to change. I know I can change all the TextView styles using style attribute, but I just need certain views get certain drawables or colors as background.
I found a solution here but I cannot use it because it needs the min sdk higher than 21 while mine is 16.
Home screen
You can easily set theme like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.your_theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity)
}
Define your style in values/styles.xml like so:
<style name="AppTheme.MyTheme" parent="AppTheme.NoActionBar">
<item name="colorPrimary">#color/primaryColorCyan</item>
<item name="colorPrimaryDark">#color/primaryDarkColorCyan</item>
<item name="colorAccent">#color/secondaryColorCyan</item>
</style>
And then call setTheme(R.style.MyTheme) before setContentView() in onCreate() method. setTheme introduced in API LEVEL 1.
Related
Background
I'm trying to prepare dark theme for an app, and one of the requirements is to have a specific color for cards and dialogs : #ff3c454c
The problem
Whether I set it by force ( app:cardBackgroundColor="#3c454c") , by reference ( app:cardBackgroundColor="#color/...) or just in the theme - in all cases I don't get the color I've set. Instead I get the color of #525A61.
I've tested just a red color (#f00) just to be sure it affects the card, and it does, and for this color it indeed gets to be fine. But for the color I've set, it doesn't.
What I've tried
As I wrote, I tried multiple ways to set the color. In the beginning I wanted to use just the theme itself, so I've set it as such:
styles.xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...
<item name="colorBackgroundFloating">#color/colorBackgroundFloating</item>
<item name="colorSurface">#color/colorBackgroundFloating</item>
</style>
res/values-night/colors.xml
<color name="colorBackgroundFloating">#ff3c454c</color>
Later I tried to use the color directly and even set it as hard coded. Still got the incorrect color when it gets shown.
Seeing this could be a bug on the library itself, I've reported about this here (include a sample project, if you wish to check it out).
I've noticed the exact same issue occurs for BottomNavigationView and probably other similar cases.
The question
Why does it occur?
Is there any workaround for this?
Something that will fix it globally for all views that use these attributes ?
What you are seeing is the elevation overlay that they introduced to make elevation more noticeable while in Dark Theme, where shadows are not so visible. You can read about it here : https://material.io/develop/android/theming/dark/ in the section "Elevation Overlays"
The simple solution if you don't want this behavior is to add this to your theme.
<item name="elevationOverlayEnabled">false</item>
And you can also adjust it to another color or even a more subtle version of the overlay by changing the alpha:
<item name="elevationOverlayColor">#80FFFFFF</item>
EDIT with more info from https://github.com/material-components/material-components-android/issues/1133
If you want to disable it for only one component or widget, you can define a style for it with a theme overlay and use it in a specific layout:
<style name="ThemeOverlay.MyApp.ElevationOverlayDisabled" parent="">
<item name="elevationOverlayEnabled">false</item>
</style>
<style name="Widget.MyApp.CardView" parent="Widget.MaterialComponents.CardView">
<item name="materialThemeOverlay">#style/ThemeOverlay.MyApp.ElevationOverlayDisabled</item>
</style>
And if you want to disable it for all cards in your app but keep it in other components you can set that style as the default style for material card views:
# Set in your app theme
<item name="materialCardViewStyle">#style/Widget.MyApp.CardView</item>
My calculator app consists of 30 buttons. I want to provide themes for the calculator keypad. A theme changes button background (gradients, not image backgrounds) and font. Some themes have the same color for all buttons while some have a color for numbers, another color for operators and so on.
The color change is using selectors from res/drawable/*.xml
How do I change the theme via the code?
Hopefully avoiding typing:
button.setBackground(Drawable background);
button.setTypeface(font);
30 times. And if I have 5 themes, then 30 * 5 * 2 = 300 lines of codes!!
I'm new to this and if there is no other way I'll go with the 150 lines.
Also how do I save the user theme selection? Using preferences?
You can create a custom XML theme which will change all of your XML components. After creating a new theme, go into the Android Manifest file and change the theme. For example:
<activity
android:name="com.myapp.MyActivity"
...
android:theme="#style/MyCustomTheme" />
To create the theme, go to res/values/themes.xml and create a new theme with an identifier:
<resources>
...
<style name="MyCustomTheme" parent="android:style/Theme">
<item name="android:textColorPrimary">#ffff0000</item>
</style>
...
</resources>
By using this method, you can create an extensive library of different themes and change to what theme you want.
NOTE: This is not just for changing the background, but it can also be used to change the theme of the buttons. Visit this website for more information:
http://janrain.com/blog/introduction-to-android-theme-customization/
EDIT: As that user commented, it is possible that you can put the function to change the theme of the button in a for() loop.
For your case, I have derived this from the link above. It will change the texture of the buttons in your XML file rather than in Java.
"Using a Custom Nine-Patch With Buttons
A nine-patch drawable is a special kind of image which can be scaled in width and height while maintaining its visual integrity. Nine-patches are the most common way to specify the appearance of Android buttons, though any drawable type can be used.
Example nine-patch PNG.
Notice the one pixel black lines around the edge, they control the scaling of the image.
Save this bitmap as MyApplication/res/drawable/my_nine_patch.9.png
Define a new style (you can define the new style in the same file that you defined your custom theme from Creating a Custom Android Theme above) …:
<resources>
...
<style name="MyCustomButton" parent="android:Widget.Button">
<item name="android:background">#drawable/my_nine_patch</item>
</style>
...
</resources>
Apply the new button style to the buttonStyle attribute of your custom theme:
<resources>
...
<style name="MyCustomTheme" parent=...>
...
<item name="android:buttonStyle">#style/MyCustomButton</item>
</style>
...
</resources>
Now the buttons in the activities your theme is applied to have custom images. However, you may notice that they don’t change appearance when selected. Read Selector Drawables below for an introduction to using multiple drawables to define one drawable that changes based on state."
From here, you can change certain components of the theme (such as the button texture as an image).
After you have a theme that looks good, apply it in the Android Manifest as I mentioned above.
I will FURTHER edit this if it still does not answer your question.
In my AndroidManifest file i do not declare a theme.
The result is:
black background and ABS with blue background, also states of list item's is blue.
thats fine.
now i want to make to set the indeterminateProgressStyle to Widget.ProgressBar.Small
Therefore i have to declare my own style like this:
<style name="Custom" parent="??">
<item name="android:actionBarStyle">#style/ActionBarIPS</item>
</style>
what should i enter in the parent parameter?
i want all style behaviors like before (black background with blue ABS and blue list item states etc as it is defined when i dont declare a theme attribute in AndroidManifest.
EDIT:
i also need to know this parent's value:
<style name="ActionBarIPS" parent="ABS with blue background">
<item name="android:indeterminateProgressStyle">#style/IndeterminateProgress</item>
</style>
the version without a style in manifest:
the version with custom style and parent=Theme.Sherlock
i want the first version with indeterminate spinner set to "small"
It's depend to your current style, It can be Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.ForceOverflow and etc, e.g:
<style name="Custom" parent="Theme.Sherlock or Theme.Sherlock.Light">
<item name="android:actionBarStyle">#style/ActionBarIPS</item>
<item name="android:indeterminateProgressStyle">#style/IndeterminateProgress</item>
</style>
Note: You must declare this style in style.xml in your values directory.
Edited:
You got blue ActionBar without using ABS because you're using Samsung TouchWiz default UI.
If you install your APK in non-samsung devices you won't see this blue action bar, But If you are forced to have blue actionbar then put the following image in your drawable directory and set it as your actionbar background through:
getSupportActionBar().setBackgroundDrawable(getResources()
.getDrawable(R.drawable.TouchWiz_ActionBar_Bg));
Try to use "Theme.Sherlock" as a parent. Also I suggest to add:
<item name="actionBarStyle">#style/ActionBarIPS</item>
I would like to change the whole application's text and background color in Java, is this possible? With this I mean to change the color of every item in the application (TextViews, ListView items, everything).
Is this possible?
I have tried using a custom-made style but I can't make it work. Here is the xml file (put in the res/layout/values folder):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Light">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
Let's say I just want to change the text color for now.
Now I call this style in my application like this:
public void onCreate(Bundle icicle) {
Activity.setTheme(android.R.style.light);
super.onCreate(icicle);
setContentView(R.layout.main);
But I get the error light cannot be resolved or is not a field.
Update:
One way I found to do this programmatically is to restart the activity, calling
this.setTheme(R.style.Light);
onCreate(null);
However, this works only for the current activity and not for the whole application. It would be great if it were possible to do this launching another activity, not only the current one.
You're trying it in a bit to simple way. Like this you're just adjusting your general Activity's background instead of all the different Views that are out there.
In order to try and adjust every type of View (Button, TextView etc) you'll need to address all their own styles to overwrite them.
Per example if you want to adjust Button you'll need in your own general style:
<item name="android:buttonStyle">#style/ButtonHoloDark</item>
This will point at your own custom style, which takes its parent from the Android's standard Button.
<style name="ButtonHoloDark" parent="android:style/Widget.Button">
<item name="android:background">#drawable/btn_default_holo_dark</item>
<item name="android:textColor">#ffffff</item>
</style>
Be warned, doing this for every View will take you quite some themes and styles.
You can find a great example how to do this exactly in theHoloEverywhere lib, which basically does the same for creating a holo theme backported to Android 2.2 or so
Finally, drop the Activity.setTheme(android.R.style.light); stuff, and just set your own theme via the manifest.
Ok so I found one possible solution, which is to pass the theme information between the activities using intents and the putExtra method.
Code for the first activity (the caller):
Intent i = new Intent(this, ActivityToCall.class);
i.putExtra("key", R.style.Light);
startActivity(i);
Code for the second activity (the called one):
public void onCreate(Bundle icicle) {
int theme = getIntent().getIntExtra("key",-1);
this.setTheme(theme);
super.onCreate(icicle);
// other code...
I don't know if it's the best possible approach but at least it works.
The attributes you want change are:
<style name="AppTheme" parent="android:style/Theme.Holo">
<item name="android:textColorPrimary">...</item>
<item name="android:textColorSecondary">...</item>
</style>
There are a few other ways to change them: Please Refer to this information
You can then set this Theme via the Manifest or setTheme(R.style.AppTheme) in your Activity's onCreate(...)
I've got a custom layout I want to use as the titlebar of my android app. The technique found (linked at the bottom) works, but the system titlebar is displayed before onCreate() is called. Obviously that looks pretty jarring, as for a moment the system titlebar is shown, then my custom titlebar is shown:
// styles.xml
<resources>
<style name="MyTheme">
<item name="android:windowTitleSize">40dip</item>
</style>
</resources>
// One of my activities, MyTheme is applied to it in the manifest.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_activity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);
}
I could always hide the system titlebar and display my own in-line perhaps with each and every layout, but, that's not very friendly.
Thanks
http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/
I think it's a framework limitation. I had the same problem in some of my applications and the ultimate solution was for me to tell the framework I didn't want a title bar at all and then create my own in my layouts. The include directive made it bearable for me, e.g.:
<include layout="#layout/title" />
When I used requestWindowFeature(Window.FEATURE_NO_TITLE) in my activities, I would have the same issue, I'd see the system title bar briefly while the activity was being build when it first loaded.
When I switched to using a theme to tell the framework I didn't want a title, the problem went away and I now see my own title directly on first load. The styling is easy for that:
<style name="FliqTheme" parent="#android:Theme.Black">
<item name="android:windowNoTitle">true</item>
</style>
I know this doesn't apply to your issue with the custom title, but like ptc mentioned, if you move your custom title into style/theme definitions (which you do by overriding the system title styles in your theme), I think you'll be on the right track.
The same problem happened to me today when I was trying to custom the title. I solved it by set the android:theme to android:style/Theme.NoTitleBar in the AndroidManifest.xml, and call setTheme() to the actual theme I want in my activity's onCreate callback function.
Try creating a custom theme style in XML and then set your activity's theme attribute in the AndroidManifest.xml file.
http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme
The method through setting android:theme does not work for me, I have made it by adding the following code in onCreate() of my Activity subclass:
getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);