- How to set text color in android app for all text?
- 5 Answers 5
- How to change default text color using custom theme?
- 5 Answers 5
- Update
- Android selector & text color
- 9 Answers 9
- Android ListView Text Color
- 8 Answers 8
- Set color of TextView span in Android
- 18 Answers 18
- Background Color
- Foreground Color
- Combination
- Further Study
How to set text color in android app for all text?
I want to define a default text color for my android app.
I have a base activity class, that all activities are extended from it and I thought this might be a good place to define the colors.
If not what is a better solution? Maybe styles?
Trouble is this, all is new to me, so feel free to advise me and provide code snippets and explanations as well.
This is what my base class looks like. As you can see it’s pretty empty
5 Answers 5
As was mentioned in denis.solonenko’s answer, the correct approach would be to modify your theme.
Where you define your theme (in your themes.xml or styles.xml file), you’ll want to add something like this:
Then make sure that the theme is applied to your Activity or Application in the manifest:
You can also define:
- textColor — The default text color of any given view
- textColorPrimary — The default text color for enabled buttons and Large Textviews
- textColorSecondary — The default text color for Medium and Small Textviews
- textColorTertiary — ?
Keep in mind that many other things may override these predefined colors, such as applied styles or definitions in different resource folders.
Источник
How to change default text color using custom theme?
What I’m trying should be quite easy with themes, but I can’t find out how to: I want all text to be white by default in my app. I created a custom theme in theme.xml:
and set it for the whole application:
But labels are still black. What’s missing?
PS: How can I additionally define styles for different text sizes, to be applied per widget? Is something like that correct?
update
I took a look at themes.xml in Android SDK, it shows how to set the text style for a theme:
In my case it should work with this definition:
However, it is still not working.
Here’s another post about this same issue.
5 Answers 5
In your Manifest you need to reference the name of the style that has the text color item inside it. Right now you are just referencing an empty style . So in your theme.xml do only this style :
And keep you reference to in the Manifest the same ( android:theme=»@style/Theme» )
EDIT:
Notice I combine the text color and size into the same style . Also, I changed the name of the theme to MyTheme and am now referencing that in the Manifest . And I changed to @android:style/TextAppearance for the parent value.
When you create an App, a file called styles.xml will be created in your res/values folder. If you change the styles, you can change the background, text color, etc for all your layouts. That way you don’t have to go into each individual layout and change the it manually.
The default Android style is also called “Theme”. So you calling it Theme probably confused the program.
name=»Theme.AppBaseTheme» means that you are creating a style that inherits all the styles from parent=»@android:style/Theme.Light» . This part you can ignore unless you want to inherit from AppBaseTheme again. =
Update
A shortcoming with the above is that also disabled Action Bar overflow menu items use the default colour, instead of being greyed out. (Of course, if you don’t use disabled menu items anywhere in your app, this may not matter.)
As I learned by asking this question, a better way is to define the colour using a drawable:
Источник
Android selector & text color
I want a simple TextView to behave the way simple_list_item_1 in a ListView does. Here’s the XML:
Everything works except for the text color that (expectedly) doesn’t change in focused state. How do I make it change to textAppearanceLargeInverse ?
9 Answers 9
I got by doing several tests until one worked, so: res/color/button_dark_text.xml
And selector is the answer here as well.
Search for bright_text_dark_focused.xml in the sources, add to your project under res/color directory and then refer from the TextView as
Here’s my implementation, which behaves exactly as item in list (at least on 2.3)
In order to make it work on selection in a list view use the following code:
Apparently the key is state_activated=»true» state.
Here is the example of selector. If you use eclipse , it does not suggest something when you click ctrl and space both :/ you must type it.
You can look at for reference;
In res/color place a file «text_selector.xml»:
Then in TextView use it:
And in code you’ll need to set a click listener.
Sorry if there are errors, I changed a code before publishing and didn’t check.
I always used the above solution without searching more after this. 😉
However, today I came across something and thought of sharing it. 🙂
This feature is indeed available from API 1 and is called as ColorStateList, where we can supply a color to various states of Widgets (as we already know).
Источник
Android ListView Text Color
I am trying to set the ListView textColor to black, since I am using a white background.
Here is my MailActivity
I’m getting the background as white, but am not sure where to set the foreground to black. I’ve tried in the xml and looks like it’s not helping.
8 Answers 8
Ok, here are some things that you should be clear about:
- The background color you are setting in your xml file is of the activity and not of the ListItems you are trying to define.
- Every list item has its own layout file which should be passed or inflated in case you are using complex layout for list item.
I’ll try to explain this with a code sample:
****Lets start with ListItems layout** : save it in your res/layout folder of you Android project with say **list_black_text.xml
Well, a simple layout with a TextView to be precise. You must have an id assigned to TextView in order to use it.
Now coming to you screen/activity/chief layout, as I said you are defining background to your screen with android:background attribute. I see you have defined a TextView there as well and I suspect you are trying to define content/list item there, which is not at all needed.
Here’s your edited layout:
And lastly, most importantly, set your adapter.
Notice the layout resource which we are passing to adapter R.layout.list_black_text , and R.id.list_content which is TextView ID we declared. I have also changed ArrayAdapter to String type since it’s generic.
I hope this explains everything. Mark my answer accepted if you agree.
Messy but a good quick fix way
You can also do this with a quick fix if you do not want to go ahead with complex layout defining etc.
While instantiating the adapter declare an inner class to do this, here is the code sample:
Источник
Set color of TextView span in Android
Is it possible to set the color of just span of text in a TextView?
I would like to do something similar to the Twitter app, in which a part of the text is blue. See image below:
18 Answers 18
Another answer would be very similar, but wouldn’t need to set the text of the TextView twice
Here is a little help function. Great for when you have multiple languages!
I always find visual examples helpful when trying to understand a new concept.
Background Color
Foreground Color
Combination
Further Study
If you want more control, you might want to check the TextPaint class. Here is how to use it:
Set your TextView ´s text spannable and define a ForegroundColorSpan for your text.
Another way that could be used in some situations is to set the link color in the properties of the view that is taking the Spannable.
If your Spannable is going to be used in a TextView, for example, you can set the link color in the XML like this:
You can also set it in the code with:
Set Color on Text by passing String and color:
Set text on TextView / Button / EditText etc by calling below code:
TextView:
Get Colored String:
Set Text on TextView:
Done
There’s a factory for creating the Spannable, and avoid the cast, like this:
Here’s a Kotlin Extension Function I have for this
Use it after you have set your text to the TextView like so
Just to add to the accepted answer, as all the answers seem to talk about android.graphics.Color only: what if the color I want is defined in res/values/colors.xml ?
For example, consider Material Design colors defined in colors.xml :
Then use ContextCompat.getColor(getContext(), R.color.md_blue_500) where you would use Color.BLUE , so that:
Where I found that:
paste this code in ur MainActivity
Below works perfectly for me
Some answers here aren’t up to date. Because, you will (in most of cases) add a custom clic action on your link.
Besides, as provided by the documentation help, your spanned string link color will have a default one. «The default link color is the theme’s accent color or android:textColorLink if this attribute is defined in the theme».
Here is the way to do it safely.
Hope this will strongly help !
From the developer docs, to change the color and size of a spannable:
Источник