font management - android

I use Html.fromHtml() in my application to display bold and regular characters in the same TextView. But I have 3 different fonts for italic,bold and regular text, and I don't know how to indicate to my text view to use one or the other.
Please give me any reference or hint.
Thanks in Advance.

if I have not misunderstood you can format a single string with different <font> tag.
For instance :
String toShow = <font: italic ...>italic string</font> <font: bold ...>bold string</font>
Html.fromHtml(toShow)
choose the tag html more appropriate for this purpose. here is a list of supporte html tag.
edit: here an example of the html font tag. As alternative I think you can use SpannableString for the same purpose.

Related

How do I add bold to a custom font family then use it for strings?

I read on Stack Overflow how to create a custom bold font and a regular one:
How to create custom font family with bold font
But I'm struggling to understand how to implement this in my strings. Ordinarily you simply use <b></b> to make text bold, but this doesn't seem to work with my custom fonts. All the text remains the same.
I know that you have tried adding the bolded font to a font family, but it hasn't worked. You can try one of the ways below. It will work even if you do import a TTF font.
One way you can do it is in strings.xml
You can create a new string and then add the b to bold it like this:
<string name="my_string"><b> My String</b></string>
You will need to create one for each string that you want to bold.
Then in your layout file, you can set the android:text to one of the strings from strings.xml and you can set the android:fontFamilyto the TTF font.
You can also bold it programmatically using the SpannableString in Java. Check this article to see how to bold programmatically.
Hope it helps!

color string resource for xml layout

<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.

Android strings xml file formatting

Hy! I am working on my Android app and I want to format text so that my TextView shows this text on CONTACT US to look EXACTLY like on that website, but I dont know how to format it in strings.xml file. I have tried with basic HTML formatting putting <br> tag for new row and <a> tag for email adresses and web site, but nothing change. How to format it ? Can so large strings be saved into strings.xml ?
http://visitsplit.com/#pages?page=409
You can use \n for new lines.
Further, you can use Html.fromHtml to create a Spanned that would have basic formatting - bold, italic, links, etc.
You would then need to override the movement method to actually make links operational:
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);
You can use HTML tags, like <br> and <a>, by using:
yourTextView.setText(Html.fromHtml(getString(R.string.your_string)));
Make sure you properly escape any special characters in your strings.xml.

Android Listview - html formating of rows

is it possible to format text in rows in Listview using html? I need to use font, b, i tags
I use this code to fill ListView
setListAdapter(new ArrayAdapter<String>(MyActivity.this,R.layout.resultlist,v));
where v is a vector array filled with strings
Thanks
No, but you can use the xml attributes to format the text in a TextView. For example, you can use the android:textStyle attribute to make the text bold, or italic. So if you want bold, you can do android:textStyle="bold" or if you want both bold and italic you can do android:textStyle="bold|italic". If you want different parts of your string formatted differently, checkout this SO Post.
Well, you can use:
Html.fromHtml("text with html");
But, I think it will slow down your ListView. Try to use android styling in your R.layout.resultlist xml layout. I'm sure you can find a way to avoid html formatting.

TextView Html formatting

I have a textview which should render its content with HTML formatting. From code, this can be achieved like this,
textView.setText(Html.fromHtml(someText));
Is there a way to do this through the XML?
I ask because i do not want to set the text through the code, In my case, the text comes from a DB call and displayed through an adapter.
Yes there are a bunch of (simple) tags that are understood by TextView -- if the text is set in XML from a string resource.
So basically
<TextView text="#string/foo" .. />
It is also possible to give templates like "Hello <b>%s</b>", but here you still need to run some code to fill in the value for %s
Have a look at http://developer.android.com/guide/topics/resources/string-resource.html for formatting hints and short examples.

Categories

Resources