How to change a strings color? - android

I want to change this string to the color red
String isFalse = False;
For some reason every tutorial seemed more complicated than I expected and I don't understand them. Is there a simple way to do this? Also, would this override the color of a textview? Because I would like it to.

String is not View, so it has no color at all.
Maybe what you want is to change the appearance color of it host TextView. To achieve this you can use:
TextView text;
//the initialize of this TextView
text.setText(isFalse);
text.setTextColor(Color.Red);
The parameter of the color could be resource from your color XML values file or android.R.color resource file, or from Color class, etc.

Please use this code.
SpannableStringBuilder builder = new SpannableStringBuilder();
String isFalse = "False";
SpannableString redSpannable= new SpannableString(isFalse);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, Tru_score.length(), 0);
builder.append(redSpannable);
TextView text1 = (TextView)findViewById(R.id.textView1);
text1.setText(builder, BufferType.SPANNABLE);

It is not complicated or hard
Addressing both possibilities..
You want to change the color for your text at runtime.
that would be like TextView.setTextColor()
i.e.
falseTextView.setTextColor(getResources().getColor(R.color.red))
if you wanted a segment of the same textview to have a different color,by which I mean that the isFalse string is only apart of the content of your TextView ,you need to use as mentioned in the other answer.

Try this...
string.xml
<string name="isFalse"><![CDATA[<b><font color=#FF0000>False</b>]]></string>
MainActivity.java
TextView textView1 = (TextView)findViewById(
R.id.textView1);
textView1.setText(Html.fromHtml(isFalse));
And result, you might get like this...

Related

How to set custom font to android toolbar's subtitle?

i want to get the subtitle´s textview in an android's toolbar to change it's font. Actually i'm doing it with the title, getting it on this way:
Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(toolbar);
I've tried with the same code but trying to get "mSubtitleTextView" but that's not the solution.
Thanks!!
You can get the subtitle TextView like this:
View subTitleView = toolbar.getChildAt(1);
If you don't add any views to the toolbar, his default structure is:
[0] - (TextView) title
[1] - (TextView) subtitle
[2] - (ActionMenuView) menu
Hope it helps!
Definitely not the best way, but in a pinch this will work. You will have to figure out a way to implement myTypefaceSpan though; I'm using Calligraphy so it ties together decently for me.
CalligraphyTypefaceSpan myTypefaceSpan = new CalligraphyTypefaceSpan(
TypefaceUtils.load(this.getAssets(), "fonts/custom_font.ttf"));
public static void setToolbarSubtitle(String subtitle, Context context) {
SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append(subtitle);
sBuilder.setSpan(MainActivity.myTypefaceSpan, 0, sBuilder.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
((MainActivity)context).getSupportActionBar().setSubtitle(sBuilder);
}

Android color ProgressBar by textview

I want to set the color of my ProgressBar getting the color of the TextView.
I have not found any documentation on this.
This is my TextView: tv.setBackgroundColor(m.color);
and this is my ProgressBar:
int pr = (int)(value);
mProg = (ProgressBar)row.findViewById(R.id.progress_e);
mProg.setMax(100);
mProg.setProgress(pr);
how can I?
tv.setBackgroundColor(m.color);
mProg.getIndeterminateDrawable().setColorFilter(m.color,
android.graphics.PorterDuff.Mode.MULTIPLY);
In case you want to retrieve the TextView's color, an easy way to do it would be to set its color as its tag, retrieve, parse it and use it as the ProgressBar's setColorFilter's parameter.
//to set it:
tv.setBackgroundColor(m.color);
tv.setTag(m.color);
//to retrieve it:
int color=Integer.parseInt(tv.getTag().toString());
mProg.getIndeterminateDrawable().setColorFilter(color,
android.graphics.PorterDuff.Mode.MULTIPLY);

Formatted text in TextSwitcher?

I'm using a TextSwitcher, however the setText() method takes in a CharSequence rather than an resID integer. If I use getString(resID) the formatting is stripped out. Is there a way to get formatted text (with bold and italics) to show in a TextSwitcher?
Learn about SpannableStringBuilder, this is so useful in producing styled text.
Then just create your formatted strings like:
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append("Hi, abc!");
sb.setSpan(new ForegroundSpan(0xffff0000), 4, 7, 0); // set characters 4 to 7 to red
yourTextWidget.setText(sb, BufferType.SPANNABLE);
Edit: TextSwitcher is just a small wrapper to ViewSwitcher. Examining the sources of TextSwitcher reveals:
/**
* Sets the text of the next view and switches to the next view. This can
* be used to animate the old text out and animate the next text in.
*
* #param text the new text to display
*/
public void setText(CharSequence text) {
final TextView t = (TextView) getNextView();
t.setText(text);
showNext();
}
So, just call this instead of setText:
final TextView t = (TextView) yourWidget.getNextView();
t.setText(sb, BufferType.SPANNABLE);
yourWidget.showNext();
In order to set a formatted text to a TextView, you need to use Html.fromHtml(String). Keep in mind, Spannedimplements from CharSequence.
You can also use Resources.getText(int) to get a styled CharSequence from your application resources.

TextView: how to dynamically change the color of certain word in paragraph

How to change the color of a paragraph word by word with timer. like at first second i want to change color of The in the below paragraph, after 5th second change color of text, after 8th second change color of will be and so on....
The text will be wrapped in tags, and displayed in a monospaced font.
just use Timer and change the font color of your edit text accordingly and stop timer in focus lost.
i think you can do something like this :
Split the paragraph to words by using the method :
split(String separator);// it will return an array of Strings
//in your case you will do somthing like this
myWords = paragraph.split(" ");// the separator is the space
And then , you can use the method to colorate what ever you want of those words by using the method :
myNewParagraph.append(HTML.fromHtml("<font color...>... Your HTML Code to colorate your Text"));
and when you finish coloring each word , you update your textView to display the new text colored
Hope it helps
You can use spans to control the appearance of text. Take a look at Spannable and CharacterStyle.
Here is an example. Of course you would need to put this in some sort of timer.
Spannable spannableText = getSpannableText(yourTextView);
spannableText.setSpan(new TextAppearanceSpan(...), wordStart, wordEnd)
yourTextView.setText(spannableText);
private Spannable getSpannableText(TextView tv) {
CharSequence cs = tv.getText();
if (cs instanceof Spannable) {
return (Spannable)cs;
} else {
return SpannableString.valueOf(cs);
}
}

Android: Statically underline dynamic text in TextView

I would like to make a TextView to be entirely underlined but I can't use a text resource and <u> tag because it is dynamic text.
Related: Can I underline text in an android layout?
So far the only way I know to do this is at runtime. Is this really the only way? Is there a way I could do it in the XML files?
The easiest solution is probably to create a custom UnderLineTextView component deriving from TextView, override setText() and set the entire text as underlined, something like this (underline code from the link you referred to above):
#Override
public void setText(CharSequence text, BufferType type) {
// code to check text for null omitted
SpannableString content = new SpannableString(text);
content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
super.setText(content, BufferType.SPANNABLE);
}
It's then just a matter of using your new component in the layout and setting the text as usual. The rest is handled automatically.
More info on custom components:
http://developer.android.com/guide/topics/ui/custom-components.html
You can underline text over Html.fromHtml(String source)
Example:
textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
You can also do this via the /res/values/string.xml file if you prefer: For example, in the /res/values/string.xml you could add an entry like:
<string name="createAccount"><u>Create Account</u></string>
And then in the onCreate(Bundle savedInstanceState) method of your activity you would add the following code to cause "Create Account"> to appear as underlined in the UI that you set for the createAccountText TextView that you defined in the xml file in /res/layout/ for your activity:
TextView createAccountText = (TextView) findViewById(R.id.createAccountText);
Resources res = getResources();
CharSequence styledText = res.getText(R.string.createAccount);
createAccountText.setText(styledText, TextView.BufferType.SPANNABLE);
peter3 wrote before to extend TextView class and override setText method.
This solution won't work as setText method is marked as FINAL.
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
Not sure if the code could work with some modifications.
SpannableString content = new SpannableString(name);
content.setSpan(new UnderlineSpan(), 0, name.length(), 0);
geometrical_textview.setText(content, TextView.BufferType.SPANNABLE);

Categories

Resources