This may be a ridiculous question ever, but still I want to reduce my effort of creating a lot of text views.
I have text view that contains Name:Value format [Suppose Name:Android] In this case all the attributes for the text view will be same except the color and also the texts are side by side.
In real implementation I have to create two text views, and suppose if I have around 10-15 such pairs, the number of text views will be 20-30 respectively.
So how can I set different color for name and value independently??
Use something like
String str = "<font color=#900000 >Name:</font> <font color=#0000FF>Android</font>";
textview.setText(Html.fromHtml(str));
You can set the text to use html tags to set colors inside the text
String formattedText = "<font color=\"#ff0000\">red</font> <font color=\"#00ff00\">green</font>";
Spanned result = Html.fromHtml(formattedText);
view.setText(result);
Or, use spannable like in Set color of TextView span in Android
Related
I want to add second textview, just after completion of the first one. Linear layout with orientation horizontal didn't help.
Any other alternatives? Please suggest!
Generally, we use HTML text for this information.
We can use as many text styles as we want for different texts. With this way, you can set properties for each texts differently.
For Example: <HTML><text1><text2></HTML>
In the above, you can define text properties independently. Finally place this text in Textview. So we can achieve this using single TextView itself.
Example:
String str = "<a>This is a <font color='#800000FF'> blue text</font> and this is a <font color='red'> red text</font> </a>";
TextView textView = (TextView)findViewById(R.id.text);
textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));
I have a question is there a way to set text the extra bold weight in a string resource?
this is what I have now:
<string name="home_text2>
<![CDATA[
some text<br/>
more text<br/>
<b>even more text</b>\u1433
]]>
</string>
The designer says all text should be in bold not just as i have it now in example string. which is easy setting android:textStyle="bold" in the text view. But how do I set part of the text in the example string as extra bold?
Been more specific I want to set the extra bold weight to the part of the text that now is just bold.
Thanks!
Nothing involving string resources or TextView support "extra bold" out of the box. You are welcome to try creating a custom CharacterStyle that implements "extra bold" by some means. Or, use a WebView. Or, draw text directly to a Canvas, with painting rules that implement "extra bold".
I want to Justify the text into textView. I does not want to use web view. because i can't increase the text size at run time for different screens. So I want To use the text view.
I want some things like that.
You can set HTML on a TextView.
So...
String htmlString = "<p align=\"justify\">This is some text in a paragraph.</p>";
mTextView.setText(Html.fromHtml(htmlString));
Good Luck. :)
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
or
<string name="some_text">this is <font color="#ffff0000">red</font></string>
is not doing it.
I need to call the string in an xml TextView. How do I get it to work? No I don't want to use a horizontal LinearLayout with a bunch of TextViews.
You can't do this. instead of you can set TextView text as HTML like
textview.setText(Html.fromHtml("this is<font color=\"##ffff0000\">red</font>"));
Go to this for more HTML tag support information :html-tags-supported-by-textview
TextView doesn't support entering HTML text directly, though some of the other answers seem to show you how to get a Html object from the String. If you want to format the text within a TextView you need to use Spannable instances to format the sections of text to have different colours, fonts etc. This is only good if you don't already have your text in HTML though, of course.
Is it possible to have a black background around each letter (not for the whole word) in a word in a TextView?
The only way I can think of is by using html in your textView.
Put your text in HTML and then do something like this
TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
String desc = "<font color=\"red\"><a href='http://www.mysite.com/'>Visit my site</a></font>";
contactWeb1.setText(Html.fromHtml(desc));
The above code is from the 3rd link
Here are some related questions
Is it possible to have multiple styles inside a TextView?
How to display HTML in TextView?
Android Linkify links textColor ignored, css style overrides possible?