2

i want to put large text inside textView. The text has multiple section with multiple colours. i have seen Html.fromHtml() function and also know how to use setSpan(). But any of these not work for me. In my case i dont know the id of text View at runtime. As i am inflating different Views at runtime each View has many textViews. It will be better if i can find a way to set styles in strings.

Please some one help me out i have spend so much time here.

Fosco
  • 38,138
  • 7
  • 87
  • 101
  • Could you explain a bit more clearly what exactly you are trying to do? – hfitzwater Mar 30 '11 at 05:21
  • @Fosco I have some lists of options in a table View on click of any of the listItem i want to show some text content. The content is raw text. there is no similarity in the different contents So i cant create a generalised layout. I have created a activity for the list and one sub Activity for showing the content. in same sub Activity i want to show any of the content. For doing so i am inflating the different layouts according to user selection. – Gaurav Mehta Mar 30 '11 at 05:38

2 Answers2

0

you can set id of text view weather the view is find by inflater

when you get text view using inflater then you can set id of that particular text view and you can set textSize() and other attributes

Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • i am inflating the parent layout(not textView) where the textViews are residing. (all the textViews are getting data from strings). – Gaurav Mehta Mar 30 '11 at 06:29
  • You can inflate the child View for example if View v = inflater.inflat(R.layout.test,null) then you can get textview from this in this wany TextView tv = (Textview)v.findviewbyId(R.id.tvName); so you can then set id of textview like tv.setId(Id); – Dharmendra Mar 30 '11 at 08:52
0

This method creating spannables directly with the attributes you need will work.

Basically you can set colors like this, given a TextView called text:

String greeting = "Hello World!"
SpannableString str = SpannableString.valueOf(greeting);
str.setSpan(new ForegroundColorSpan(0xffffffff), 0, greeting.length(),
    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xff0099ff), 0, greeting.length(),
    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
text.append(str);
Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • This looks good but here the problem is that in my case i am inflating the parent layout(not textView) where the textViews are residing. (all the textViews are getting data from strings). Is there any way that i give html tags in string files and i do not have to call fromHtml() for textviews. – Gaurav Mehta Mar 30 '11 at 05:54
  • Yes, you can use html in your strings file. If you use the eclipse editor your html will be escaped for you automatically. – Matthew Mar 30 '11 at 05:54
  • @Matthew Willis iPrepare iPlan\n The most powerful tool in the toolkit I am doing like this bold is working but text colour is not working – Gaurav Mehta Mar 30 '11 at 06:11
  • Are you able to replicate this behavior: http://stackoverflow.com/questions/2730706/highlighting-text-color-using-html-fromhtml-in-android – Matthew Mar 30 '11 at 06:12
  • @Matthew Willis: i can't call the setText as at run time i cant decide which textView to be inflate. i want a way such that the formatting which i do in strings will not require to call from.Html all the formatting should be done in compile Time i.e not in java – Gaurav Mehta Mar 30 '11 at 06:22
  • @Matthew Willis I want to do similar things http://stackoverflow.com/questions/2730706/highlighting-text-color-using-html-fromhtml-in-android but they are using fromHtml() – Gaurav Mehta Mar 30 '11 at 06:25
  • I was asking if you are able to replicate the behavior of `Hello` using the string editor without the `` tag. – Matthew Mar 30 '11 at 15:01