I have the following TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This must be bold (but not this)" />
I want only a part of the text to be bold. android:textStyle="bold" will therefore not work.
If the text is coming from a string resource, use <b> tags as you would in HTML:
<string name="foo"><b>This must be bold</b> (but not this)</string>
If the text is coming from somewhere else, use a SpannableStringBuilder to wrap the bold portion in a StyleSpan set for boldface. Or, generate an HTML edition of the string with <b> tags and use Html.fromHtml() to create the Spanned. In either case, the Spanned/SpannableStringBuilder output then just gets passed to setText(), which will rendered the desired portion in bold.
You can do it but I think it is very handy with xml therefore you can create a string in html and do it like this:
String sourceString = "<b>" + id + "</b> " + name;
mytextview.setText(Html.fromHtml(sourceString));
Where id and name will be two different text in your class
Related
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".
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
I need my textview to have different colored texts. Also I need to do this from xml code, non from java code. Is there anyone who knows some way for doing this?
Thanks
e.g. I have sentence "This is red". I need words to be green, and word red to be red.
There are three ways to change the color of some text inside a textview.
through strings.xml file in (res>values), using the tag (<![CDATA[<p>This is green <font color='hexvalue of red'>and this is red</font>.</p> ]]>) and then declaring the textview in java code as myTextView.setText(Html.fromHtml(getString(R.string.myText));
through java code, using the HTML tag String text = "<font color='hexvalue of green'>This is green</font> <font color='hexvalue of red'>and this is red</font>."; myTextView.setText(Html.fromHtml((text));
through Spannable text using java code.
Spannable span = new SpannableString("My String");
span.setSpan(new ForegroundColorSpan(Color.RED), start_position, end_position,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(span);
If there are other ways to do it then I'm not aware of them.
Hope this helps
Refer your text to string.xml and using html font tag , by using that way you can change each letter color also .
just add this in java for that string:
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml(getString(R.string.any_text)));
and
In string.xml:
<string name="any_text">
<![CDATA[ <b><font color=#ff0000>write</b> your <b><font color=#0000ff>text</b> here .
]]>
</string>
hope help you
If you want to give text color in strings.xml then check out the below code:
<string name="by_continuing_i_confirm_that_i_have_nread_the_privacy_policy">
<font fgcolor='#ffffff' >By continuing, I confirm that I have \nread the</font> <font fgcolor='#2DBBDD' >Privacy Policy</font>
In Java class define TextView like this:
TextView tv = (TextView) findViewById(R.id.text1);
String text = "<font color=#cc0029>write any thing here</font> "+
"<font color=#ffcc00>write any thing here 2</font>";
tv.setText(Html.fromHtml(text));
<TextView
android:id="#+id/yourUniqueTextViewID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#color/RED" />
Where "RED" is a named constant you have to define under res/values/ in an xml file. Typically i create "colors.xml".
Or see this for a good set of predefined colors: Web colors in an Android color xml resource file
I had two strings:
<string name="bigtext">Big text</string>
<string name="anothertext">Small text</string>
I need them both in EditText's android:hint.
I tried to combine them & put "Big text" into <b>Big text</b>:
<string name="test">"<b>Big text</b> \nSmall text"</string>
"Big text" is bold, yep. But how to specify that "Big text" should have bigger font than "Small text"?
Add <big> tags around the big text. Or, if you prefer, add <small> tags around the small text. However, those will not work in a string resource AFAIK -- you would need to have this string in Java code or something, using Html.fromHtml() to get the SpannedString you would feed to setHint() on EditText.
I know how to set the Text Style bold. But is there any Shortcut as like <B> tag in HTML that can make Some words as bold.
Ex. :
phone:9825056129
In above text i want to set the phone as bold and the remaining as the normal text. So is it possible ?
Use setspan as showun in following link
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/text/Link.html
Yes. Write your text as HTML and then use Html.fromHtml to get it into a form that is understood by TextView
E.g.
TextView mtv = ...
mtv.setText(Html.fromHtml(s + " <b>" + status.getInReplyToScreenName() + "</b>"));