While looking through the built in Android themes/styles I noticed references to textColorPrimary, textColorSecondary etc. I would like to implement this in to my own theme but cannot find any way of setting a textview (or any view) to "primary" or "secondary" etc.
Am I overlooking something or is it not possible to be done?
The references you're seeing to "textColor_____" are references to color selectors. You use them with a line like 'android:textColor="?android:textColorSecondary"' in the xml definition of your TextView.
See this for more detail:
http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes
Related
I've created the project from default template Bottom Navigation Activity. I found that the background color for BottomNavigationView is #2D2D2D for dark theme. I defined it in colors.xml, but I'm not sure that it's good solution. Is there any pre-defined colors in system(smth like #android:color/theColorThatINeed)? Where can I see all of them?
the best practice is to use colors.xml, and put there all of your colors,
and then call it like this:
android:background="#color/red"
there are also system constants for colors, which contains this constants:
http://developer.android.com/reference/android/R.color.html
this resources are read only and you can not add to them more constants.
Unfortunately there isn't a single place to find all the 'system colours'. You can either find them through documentation, which is usually easy with the material components (which is where BottomNavigationView is from), or by trawling through the source code. For your particular example the colour you're after is ?attr/colorSurface, which is in the documentation and the source code.
Is there any pre-defined colors in system?
Yes, there is but it's limited. These colors are defined in android.R.color. Here are some of them.
background_dark
background_light
black
darker_gray
holo_blue_bright
holo_blue_dark
holo_blue_light
holo_green_dark
holo_green_light
holo_orange_dark
holo_orange_light
holo_purple
holo_red_dark
holo_red_light
transparent
white
widget_edittext_dark
To use them:
android:background="#android:color/background_dark"/>
Can anyone explain the question mark means in Android XML attributes?
<TextView
style="?android:attr/windowTitleStyle"
More attributes
/>
The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.
\? escapes the question mark.
The ? lets you refer to a style attribute instead of a specific hard-coded resource. See "Referencing style attributes" in the Android Dev Guide for details.
So, how is this actually useful? It makes the most sense when considering multiple themes containing the same custom resource attribute.
Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And that movieIcon attribute points to a different #drawable resource, say robot.png or hobbit.png, in each theme definition.
You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. You don't need any theme-dependent logic to use the different drawables. You just define the movieIcon attribute for each theme and the Android framework takes care of the rest.
using:
android:textAppearance="?android:attr/...."
I would like to style a TextView to look like the default style of the "summary" attribute within a PreferenceScreen.
using the styles from this official list, I have tried "summary" and "summaryOn", but neither of them looks like the default style for summaries. Can anyone explain why, or suggest which one I should be using?
There isn't a theme attribute that refers to the specific semantic of preference summary text, preferences use ?android:attr/textAppearanceSmall as illustrated here: http://goo.gl/3yZmx (This is the layout file used by the Android framework for preference items.)
The correct way to reference an ID is using #.
Such as #string/my_string
Since you wish to reference an ID defined by Android directly, try this:
android:textAppearance="#android:id/summary"
Can anyone explain the question mark means in Android XML attributes?
<TextView
style="?android:attr/windowTitleStyle"
More attributes
/>
The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.
\? escapes the question mark.
The ? lets you refer to a style attribute instead of a specific hard-coded resource. See "Referencing style attributes" in the Android Dev Guide for details.
So, how is this actually useful? It makes the most sense when considering multiple themes containing the same custom resource attribute.
Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And that movieIcon attribute points to a different #drawable resource, say robot.png or hobbit.png, in each theme definition.
You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. You don't need any theme-dependent logic to use the different drawables. You just define the movieIcon attribute for each theme and the Android framework takes care of the rest.
I have a custom component that I want to give the same colors as a TextView.
That is, I don't want to copy its colors, I want to get the default background and foreground colors, if there's such a concept on android.
[Edit]
The following seems to yield the text color of my TextView. But is it just luck? It's not intuitive to me that a default TextView would use android.R.attr.textColorSecondary? And why does not resolveAttribute return the color directly?
TypedValue tv = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.textColorSecondary, tv, true);
Color holyColor = getResources().getColor(tv.resourceId);
[Edit]
I found the source code of TextView at android.git.kernel.org, but it seemed to contain a lot of referrences to com.android.internal.R, which I don't think I should use in my own code. I'm currently looking for some kind of evidence that TextView uses android.R.attr.textColorSecondary.
[Edit]
I found some kind of evidence at developer.android.com, in styles.xml that TextView uses android.R.attr.textAppearanceSmall. textAppearanceSmall is documented to default to "secondary text color".
I guess I was lucky after all, but I still don't like that little code snippet of mine.
What you're looking for are attributes. Attributes link widgets to styles. For example, android:background is what you'd set for a particular view, but there's attributes like android:panelBackground and android:windowBackground that you can use override to affect the system as a whole.
You should look through R.attr and then link to those attributes in your widget. There should be a number of them that are linked to TextView; it would make sense to download the Android source code and see which attributes are used.