How to span a textview in layout itself in android? - 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

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 .

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