Android Listview - html formating of rows - android

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.

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!

Android textview different style for bold text

I have a long string, with multiple bold words inside the string.
The styling of the non bold sentences are done inside the TextView XML part and this is looking fine.
But i want to make the bold words bigger and thicker.
Is this possible to change this with XML?
for example:
<string name="long_text">This should be a <b>long</b> text with different <b>styling</b></string>
The bold words are supposed to be 1.5 times bigger
Or should i do this in the Java code?
yes you have to do this in java because there is no way to as per your requirement,
you can use TextAppearanceSpan in java code with Spannable
if i am getting you right than try the following..
textview.setText(Html.fromHtml(getResources().getString(R.string.long_text)));
Mark as right if it works for you .

How to span a textview in layout itself in android?

Can I span a textview to make some part bold in the layout itself. I have around 20 xml files. I am not using any activity for them, so I cannot do it programatically. So is there a way to specify it in xml layout itself. I have already tried using between them but it doesn't works.
android:text="Lets look how to make <b> negative sentences <b> in simple present tense."
doesn't work.
You can style a textView in xml but I believe it has to be a string resource rather than a hardcoded value like you are doing. Move the string to strings.xml and access it using resource id
android:text="#string/name"
Check this post.
Quoted from the post:
Supported HTML elements include:
<b> for bold text.
<i> for italic text.
<u> for underline text.
Check here
http://developer.android.com/reference/android/text/Spannable.html
or
https://sites.google.com/site/androidhowto/style-text
or
http://www.java2s.com/Code/Android/2D-Graphics/UsingSpannabletextandsetstyle.htm

font management

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.

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