Multiple color text in TextView?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Multiple color text in TextView?

Postby iifuzz » Tue May 26, 2009 9:06 pm

Is there any way to get multiple color text in any of the widgets?

Im trying to make a client app which will display chat text similar to this...

Name: Message
iifuzz
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu May 21, 2009 6:00 pm

Top

Postby SlumberMachine » Wed May 27, 2009 9:00 am

Yes it is possible. In a TextView you need to use these imports:

Code: Select all
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;


I found this example on the open source google code pages, they were using it to format log output on the screen for multiple colors. Sorry I haven't figured it out any further yet, but I know this code below holds the key to doing it:

Code: Select all
       
private static class LoglineFormattedString extends SpannableString
{
public static final HashMap<Character, Integer> LABEL_COLOR_MAP;
public LoglineFormattedString(String line)                 
{
super(line);
try
{
Integer labelColor = LABEL_COLOR_MAP.get(line.charAt(0));
if (labelColor == null) labelColor = LABEL_COLOR_MAP.get('E');     
setSpan(new ForegroundColorSpan(labelColor), 0, 1, 0);                                 
setSpan(new StyleSpan(Typeface.BOLD), 0, 1, 0);                                                               
{
setSpan(new ForegroundColorSpan(labelColor), 2, leftIdx, 0);
setSpan(new StyleSpan(Typeface.ITALIC), 2, leftIdx,0);
}
}catch (RuntimeException e)
{
setSpan(new ForegroundColorSpan(0xffddaacc), 0, length(),0);
}
}
static { LABEL_COLOR_MAP = new HashMap<Character, Integer>();
LABEL_COLOR_MAP.put('D', 0xff9999ff);
LABEL_COLOR_MAP.put('V', 0xffcccccc);
LABEL_COLOR_MAP.put('I', 0xffeeeeee);
LABEL_COLOR_MAP.put('E', 0xffff9999);
LABEL_COLOR_MAP.put('W', 0xffffff99);
}
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Postby iifuzz » Wed May 27, 2009 1:15 pm

thanks ^_^ ill try playing with this today

im still learning Java as i go so it may take me a while.
iifuzz
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu May 21, 2009 6:00 pm

Postby iifuzz » Wed May 27, 2009 2:07 pm

ok, i wasnt to sure how to use seperate classes in java yet and all that so i ghetto rigged it and this worked!

text.setText("");
text.append("\n " + Name + ": " + LsMessage);
Spannable sText = (Spannable) text.getText();
sText.setSpan(new ForegroundColorSpan(Color.RED), 1, Name.length() + 3, 0);
ChatLog_Txt.append(sText);

This marks the persons name Red and leaves the rest white ^_^
iifuzz
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu May 21, 2009 6:00 pm

Postby shishir.bobby » Thu Aug 13, 2009 7:10 am

hi there, :)
u can achieve coloring through xml also.... below is small piece of code demonstarting how to color texts...have a look

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <AbsoluteLayout
  5.  
  6. android:id="@+id/widget0"
  7.  
  8. android:layout_width="fill_parent"
  9.  
  10. android:layout_height="fill_parent"
  11.  
  12. xmlns:android="http://schemas.android.com/apk/res/android"
  13.  
  14. >
  15.  
  16. <TextView
  17.  
  18. android:id="@+id/username"
  19.  
  20. android:layout_width="wrap_content"
  21.  
  22. android:layout_height="wrap_content"
  23.  
  24. android:text="USERNAME"
  25.  
  26. android:textColor="#CC0000"
  27.  
  28. android:layout_x="41px"
  29.  
  30. android:layout_y="65px"
  31.  
  32. >
  33.  
  34. </TextView>
  35.  
  36. <TextView
  37.  
  38. android:id="@+id/password"
  39.  
  40. android:layout_width="wrap_content"
  41.  
  42. android:layout_height="wrap_content"
  43.  
  44. android:text="PASSWORD"
  45.  
  46. android:textColor="#ffff00"
  47.  
  48. android:layout_x="43px"
  49.  
  50. android:layout_y="107px"
  51.  
  52. >
  53.  
  54. </TextView>
  55.  
  56. <TextView
  57.  
  58. android:id="@+id/shishir"
  59.  
  60. android:layout_width="wrap_content"
  61.  
  62. android:layout_height="wrap_content"
  63.  
  64. android:text=":   shishir"
  65.  
  66. android:textColor="#6600ff"
  67.  
  68. android:layout_x="142px"
  69.  
  70. android:layout_y="68px"
  71.  
  72. >
  73.  
  74. </TextView>
  75.  
  76. <TextView
  77.  
  78. android:id="@+id/pw"
  79.  
  80. android:layout_width="wrap_content"
  81.  
  82. android:layout_height="wrap_content"
  83.  
  84. android:text=": *****"
  85.  
  86. android:layout_x="144px"
  87.  
  88. android:layout_y="111px"
  89.  
  90. >
  91.  
  92. </TextView>
  93.  
  94. </AbsoluteLayout>
  95.  
  96.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4





I found this way is more simpler....


thanks and regards
shishir 8) 8) 8)
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests