Shortcut for making text Style bold - android

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>"));

Related

How to make some part of text bold and some part of text normal in both EditText and TextView

I want to make something like
wow its cool app
in android. so that users can see a bold text in both TextView and EditText like WhatsApp. I don't know what to do. I searched and found something in which we can use HTML tags but some of the users don't know how to use that. Is there a way so that it is easy for users to make some part of text bold and remaining will normal.
Here is the image which will clear more
in this image, users can bold some part of text by enclosing that in *'s.
For you to make text bold? Place it in a StyleSpan with a bold style to it. For the user to make it bold? You'll have to provide some UI for them to do that, that isn't built in.
You have to use HTML code and set to textview or edittextview as following..
TextView textView = (TextView) findViewById(R.id.textView);
String first = "Hello what's up ";
String next = "<B>some bold text </B>";
textView.setText(Html.fromHtml(first + next));
if you want to add bold text after '' character then find that character from textview or edittextview and apply tag after '' this character and set text into textview and edittextview

Android: How to use bold text inside normal text

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

Set Span style HTML text in Text View 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

Android set color and listner on part of text

Suppose I have TextView and I want to change color of some part in it. Like this
And I must set OnclickListener on text " Terms Of Use ", so How can I do that but using two textViews. Thanks
Regards
Use a simple Checkbox with no text, and for text clicking event
this is what you are looking for Spannable
You can youse HTML in TextView:
myTextView.setText(Html.fromHtml("<h2>Test</h2>"));

Background around each letter in a word in TextView

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?

Categories

Resources