Syntax for Retrieving Value from Nested Style Resource? - android

Since DrawerLayout is part of the chrome of an Android activity, a likely background color for the drawer would seem to be the background color of the action bar, so they match. Hence, I'd like to set up the ListView that is the drawer contents to have the same background color as the action bar, and I'd like to do that in the layout XML that defines the ListView.
And here I get lost in the maze of twisty little passages that is the Android style system...
I know that ?android:attr/ syntax allows you to refer, by reference, to a value defined in the theme being used by the activity (e.g., ?android:attr/activatedBackgroundIndicator as the background for a list item row to work with the "activated" state).
I know that android:actionBarStyle is where a theme points to the style to be used to style the action bar itself, and on that nested(?) style, android:background is the background used for the action bar.
What I don't know is how to craft an ?android:attr/, to be applied to a ListView, that pulls the background from the action bar's defined style.
Is this possible? If so, what's the syntax?
My guess is that this is not possible, which is why the official DrawerLayout sample hard-codes the background color...
Thanks!

It's not possible unfortunately via XML.
You could do the following in code (untested, but should work):
// Need to manually create android.styleable.ActionBar.
// If you need other attributes, add them
int[] android_styleable_ActionBar = { android.R.attr.background };
// Need to get resource id of style pointed to from actionBarStyle
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.actionBarStyle, outValue, true);
// Now get action bar style values...
TypedArray abStyle = getTheme().obtainStyledAttributes(outValue.resourceId,
android_styleable_ActionBar);
// background is the first attr in the array above so it's index is 0.
Drawable bg = abStyle.getDrawable(0);
abStyle.recycle();

AFAICT it's simply not possible to use any ?android:attr syntax in order to address actionbar background, simply because there is no such attribute exported in the related attrs.xml
You should add/define such attribute in your custom theme, then use a reference in your styles/layouts. Eg:
-attrs.xml:
<declare-styleable name="CustomTheme">
<attr name="actionbarBackground" format="reference"/>
</declare-styleable>
-themes.xml
<style name="CustomTheme" parent="Theme.Holo.Light">
...
<item name="actionbarBackground">#color/your_fav_color</item>
<item name="android:actionBarStyle">#style/CustomActionbar</item>
...
</style>
-styles.xml
...
<style name="CustomActionbar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">?attr/actionbarBackground</item>
....
</style>

Related

change color of android

I know this question must be asked before, but I don't know where can I find the solution since I don't know what is the name.
I want to change the color to red. How can I do to achieve this ?
Thanks a lot !
A style on Android contains a number of different attributes. This can control background color, text color, font styling, etc.
The Theme.AppCompat.Light.DarkActionBar style contains several attributes for an overall light theme with a dark colored action bar.
If the background color you want to use needs white text and icons on top of it, you reference this as the parent and just change the background color.
<item name="android:background">#ff0000</item>
If your background color would look better with black (or dark) text, you could set the parent to Theme.AppCompat.Light.
If you want to change the color of the application bar (and the application's primary color – the application bar inherits it), just change your colorPrimary attribute. Like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/yourColorResource</item>
</style>
Instead of #color/yourColorResource, you can directly use a color's hexcode or Android's pre-defined resources such as #android:color/holo_red_dark
For the status bar's color, you can use similarly use the colorPrimaryDark attribute. You can read more about this here.

Action bar Background Color Not Changing

I have a style file where i am setting some action bar style like this
<style name="ActionBar.Solid.Pro" parent="#style/Widget.Sherlock.ActionBar.Solid">
<item name="background">#color/dialer_color</item>
<item name="backgroundStacked">#color/dialer_color</item>
<item name="backgroundSplit">#color/dialer_color</item>
<item name="android:progressBarStyle">#style/ProgressBar.Pro</item>
</style>
Now i want to change the color of action bar programmatically like this`
getSupportActionBar.setBackgroundDrawable(new ColorDrawable(Color.Red));
But it do not have any effect. Can you please tell me why , where i am doing wrong
Is it possible to change the ActionBar.TabView background programatically ?
I am setting my style like this, how can i change the background of this style attribute ?
<style name="ActionBarTabStyle.Pro" parent="#style/Widget.Sherlock.ActionBar.TabView">
<item name="android:background">#color/dialer_color</item>
</style>
See you can refer to android action bar style generator
That makes the work of customising the action bar much easier
I advice you that you must have a look on this link
It will surely help you
http://jgilfelt.github.io/android-actionbarstylegenerator/#name=example&compat=holo&theme=light&actionbarstyle=solid&texture=0&hairline=0&neutralPressed=1&backColor=E4E4E4%2C100&secondaryColor=D6D6D6%2C100&tabColor=33B5E5%2C100&tertiaryColor=F2F2F2%2C100&accentColor=33B5E5%2C100&cabBackColor=FFFFFF%2C100&cabHighlightColor=33B5E5%2C100
And one more thing when you will be done with specifying the colors then you will receive the set of images in an archive i.e. .zip format
So you must extract them in their respective folders or directories
In the directory naming "res"
My suggestion is to drop the ActionBar alltogether and use the new Toolbar from the AppCompatLibrary V21. There you just specify Primary and Accent Colors and your background will be set, as well as all controls on it.
For starters this is described in the blog from Chris Banes:
appcompat v21: material design for pre-Lollipop devices!

Use appropriate colors irrespective of themes

I want my activity to have two possible themes, say Theme_Holo and Theme_Holo_Light, as selected by the user. I need to programmatically draw things like horizontal dividers in this activity. The color of the divider should depend on the selected theme. How can I do that easily?
Ideally there should be a name for the standard color of a divider irrespective of the theme used, and the actual RGB realization of that color name would match the selected theme automatically. Is there such thing? It seems unlikely to me that the programmer needs to hardcode RBG values.
Of course, the divider is only an example. I would also like to name the color of EditText, or other widgets, in a way that does not depend on the theme.
In your Activity's onCreate() method(s), before calling setContentView(), set the theme using this.setTheme(customTheme);
or give it a try, Using Themes in Android Applications
You can create your own theme attributes and use them in your app's themes, and allow the user to switch between your app's themes.
First, make a file called attrs.xml in /res/values folder and define some theme attributes:
<resources>
<attr name="myDividerColor" format="color" />
</resources>
Next, make two themes in your /res/values/styles.xml (or in /res/values/themes.xml if you do your themes and styles separately). One theme extends Android's dark theme and one extends Android's light theme. Add your custom attributes to your themes:
<resources>
<!-- Dark theme -->
<style name="AppTheme_Dark" parent="#android:Theme.Holo">
...
<item name="myDividerColor">#color/divider_dark</item>
</style>
<!-- Light theme. -->
<style name="AppTheme_Light" parent="#android:Theme.Holo.Light">
...
<item name="myDividerColor">#color/divider_light</item>
</style>
</resources>
Note that I used name="myDividerColor", NOT android:name="myDividerColor"
Finally, in your Activity code you can get the color as follows:
// the attrs you want
int[] attrs = {R.attr.myDividerColor};
// get attr values for the current theme
TypedArray a = obtainStyledAttributes(attrs);
// first arg is the index of the array, in same order as attrs array above
// second arg is a default value (if not defined or not a resource)
int dividerColor = a.getColor(0, Color.TRANSPARENT);

override standard style with ABS

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>

Styling all TextViews(or custom view) without adding the style attribute to every TextView

Is there a way to format all TextViews, Buttons or whatever with a theme ?
Like in CSS when i want to format all a-tags
a{
/some css here/
}
I want to do that in android via xml when I'm applying a theme to my application.
Any ideas ?Thanks
http://bartinger.at/
Update 1.0:
I want to create a theme that formats the text in all TextViews green and in all EditTexts red. So that i just apply the theme and I never have to worry about the style attribute!
Update 1.1:
So I found some that piece of code and I think that's a good beginning
<item name="android:textViewStyle">#style/MyTextView</item>
<item name="android:buttonStyle">#style/MyButton</item>
I think thats the answer to my question. But I have another one. I want to write my own ActionBar and wanted to know how I can apply a default style or default attributes (again without adding the style attribute in the layout xml :P )
I have a class
public class ActionBar extends LinearLayout{ }
and I'm gonna use it like that in my application
<at.bartinger.uil.ActionBar>....</at.bartinger.uil.ActionBar>
The ActionBar should have some default attributes (like height and width) and then adding some custom style attributes which could change from app to app (like background)
yes you can you can apply a theme to the whole application and then all your textviews will have that style.
Inside the styles.xml file you have to define your CustomTheme
for example:
<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>
you add something like text
"android:textStyle="myStyle" and specify the details in Mystyle
You can apply a style read more here.
for the action abr you should look here http://developer.android.com/guide/topics/ui/actionbar.html
especially at the bottom it explains very well how to style the bar

Categories

Resources