Skip to main content

Single TextView with multiple colored text ?

Single TextView with multiple colored text ?

A1.

             TextView TV = (TextView)findViewById(R.id.mytextview01);
       Spannable word = new SpannableString("Your message");        

       word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

       TV.setText(word);
       Spannable wordTwo = new SpannableString("Your new message");        

       wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
       TV.append(wordTwo);


A2.

        You can use Spannable to apply effects to your TextView:
        Here is my example for colouring just the first part of a TextView text (while allowing you to set         the color dynamically rather than hard coding it into a String as with the HTML example!)

     mTextView.setText("Red text is here", BufferType.SPANNABLE);
     Spannable span = (Spannable) mTextView.getText();
     span.setSpan(new ForegroundColorSpan(0xFFFF0000), 0, "Red".length(),
             Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

       In this example you can replace 0xFFFF0000 with a getResources().getColor(R.color.red)


A3.


         Use SpannableStringBuilder
     SpannableStringBuilder builder = new SpannableStringBuilder();

     SpannableString str1= new SpannableString("Text1");
     str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
     builder.append(str1);

     SpannableString str2= new SpannableString(appMode.toString());
     str2.setSpan(new ForegroundColorSpan(Color.GREEN), 0, str2.length(), 0);
     builder.append(str2);

    TextView tv = (TextView) view.findViewById(android.R.id.text1);
    tv.setText( builder, TextView.BufferType.SPANNABLE);


A4.


    mBox = new TextView(context);
    mBox.setText(Html.fromHtml("<b>" + title + "</b>" +  "<br />" + 
      "<small>" + description + "</small>" + "<br />" + 
      "<small>" + DateAdded + "</small>"));

Comments

Popular posts from this blog

Reset Android textview maxlines ?

Reset Android textview maxlines  ? A1.         Actually, the way android platform does that is by setting the MaxLine to                                            Integer.MAX_VALUE. textView . setMaxLines ( Integer . MAX_VALUE );        also, if you are using Ellipsize, don't forget to set to null. textView . setEllipsize ( null );       just check how the android framework do just that ;) watch the                                                              setMaxLines(Integer.MAX_VALUE); private void applySingleLine ( boolean singleLine , boolean applyTransformation ) { mSingleLine = singleLine ; if ( singleLine ) { ...

Android set the gravity for a TextView programmatically

Android set the gravity for a TextView programmatically  A1.        This will center the text in a text view: TextView ta = ( TextView ) findViewById ( R . layout . text_view ); LayoutParams lp = new LayoutParams (); lp . gravity = Gravity . CENTER_HORIZONTAL ; ta . setLayoutParams ( lp ); A2.      textView . setGravity ( Gravity . CENTER | Gravity . BOTTOM ); A3.              Use this code TextView textView = new TextView ( YourActivity . this ); textView . setGravity ( Gravity . CENTER | Gravity . TOP ); textView . setText ( "some text

Android and   in TextView ?

Android and &nbsp; in TextView ? A1.            The TextView should respect the non breaking space <string name = "test" > Hello&#160;world </string>                                     or new TextView ( "Hello\u00A0world" ); A2.      One unique situation I ran into was adding a non-breaking space to a string resource that took               String.format parameters. <resources> <string name = "answer_progress" formatted = "false" > Answered %d of %d </string> </resources>       I tried to simply copy and past the non-breaking space character into the string and it was                     replaced       with a regular old space after compilin...