I am working on an android app i want to know how i can make few words in bold regular and few in italic for example i have a textview with a text HELLOO now I want to display the text like this in a text view
HELLOO
please tell me how to achieve this through styles in android??
Say, you have a TextView namely etx, use the following code:
final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC);Span to make text italic
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
etx.setText(sb);
The main advantage of using this approach is that you can format text dynamically.
<b> is deprecated. use <strong> and <em> instead of <i>.
text1.setText(Html.fromHtml("<strong>bold text</strong> normal text <em>italic text</em> "));
If you have String in strings.xml then.
<string name="startup"><b><i>HELL</i></b><i>oo</i></string>
You can use HTML Tags Here inside <string> </string>
If you want to make it bold use the b tag.If you want it to be italic use i tag.If u want to make a word bold or italic put that single word inside the tag.
<resources>
<string name="register"> <u><b><i>SignUp</i></b></u> </string>
</resources>
String text="<html>"+"<b>"+"Hell"+"</b>"+"<i>"+"oo"+"</i>"+"</html>";
Related
In my android app with Kotlin, I created a layout in which there's a TextView that shows some text. For the text I have an item in strings.xml where I want to change the color of part of this Text, I tried the following code :
<string name="description">the product is <font fgcolor="green"> free </font></string>
But, The color didn't change.
I just want to change the color of "free" to green, can someone explain how I can achieve this?
Use <font color="#008000">free</font> instead. According to the documentation, the correct attribute name is color and it only supports hex codes.
Ben P.'s awesome answer should satisfy your use case. However, I want to present to you another way you can achieve this.
You can use SpannableString to achieve the same effect. With SpannableString, you can set several behaviours (color, font-weight, font-size, click-behaviour, etc) to any part of your String.
For the string in your question, you can do something like this:
// the textview you want to set your coloured text to
TextView textView = (TextView) findViewById(R.id.myTextView);
// declare the string you want to span as a Spannable
Spannable wordtoSpan = new SpannableString("the product is free");
// set the colour span
wordtoSpan.setSpan(new ForegroundColorSpan(Color.GREEN), 15, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the text to your TextView
textView.setText(wordtoSpan);
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
As I am getting the html text from the service and I need to display the text on text View.
Managing Safely <b> End Of Course Theory Test </b> <span style="color:red;"> part1 </span>
I am setting this text like
tv.settext(Html.fromHTML("Managing Safely <b> End Of Course Theory Test </b> <span style="color:red;"> part1 </span>"));
It is showing bold but not showing the color red text.
As #ρяσѕρєя K told above that HTML span tag is not supported by Html.fromHtml.
You should either change the service
OR
add a following line to your code, it will change the span color html to font tags, atleast in your case.
String yourHtmlText = yourHtmlText.replace("span style=\"color:", "font color='").replace(";\"","'").replace("</span>", "</font>");
For others I'll recommend to use String.split frequently according to your needs and it will work like magic.
I hope this works.
Cheers :)
As you can see HTML Tags Supported By TextView HTML span tag is not supported by Html.fromHtml.
So you should return only supported tags from server like font,div,p,... or use webview to show all html tags
you have to use following code to show data from html
tv.settext(Html.fromHTML("Managing Safely <b> End Of Course Theory Test </b> <font color='red'>simple</font>"));
enter text seperately
TextView text = ... // find or instantinate your text view.
text.setText(Html.fromHtml("<font color='#ff0000'>text</font>"));
or use spannable string
text.setText("");
text.append("Add all your funky text in here");
Spannable sText = (Spannable) text.getText();
sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);
use this library support all html tags.
https://github.com/NightWhistler/HtmlSpanner
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 am creating one application in which I am searching for a Perticular word in text view.I have one edit text,one text view and one button,when I am entering any word in edit text and clicking on button it gives me the line position and position of word in that line from entire text file...I have append text file's contain in text view...now my question is can I highlight that all word which is there in text view entered by edit text.?if I can than please tell me how to do it..if any one have idea of it?
You can also do it by using a Spannable, which is convenient as you know the position of the word:
SpannableString res = new SpannableString(entireString);
res.setSpan(new ForegroundColorSpan(color), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
Where entireString is the string in which the word to highlight exists, color is the color you want the hightlighted text to have, start is the position of the word and end is where the word ends (start+word.length()).
The SpannableString res can then be applied to your textview like a regular string:
textView.setText(res);
Note: If you want the background of the text to get a color rather than the text itself, use a BackgroundColorSpan instead of a ForegroundColorSpan.
Edit:
In your case it would be something like this (you will have to save the value of linecount and indexfound for when you read the entire text):
for(String test="", int currentLine=0; test!=null; test=br2.readLine(), currentLine++){
if(currentLine==linecount){
SpannableString res = new SpannableString(test);
res.setSpan(new ForegroundColorSpan(0xFFFF0000), indexfound, indexfound+textword.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.append("\n"+" "+test);
}
Yes You can highlight some portion of the text view by writing HTML
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
use Spannable to set text in spannable you can setSpan by which you can highlight the text you want
string ="<font color='#ff0000' > <b>hello</b> </font>"
textview.setText(Html.fromHtml(string))
http://developer.android.com/reference/android/text/Html.html