Two-color text in TextView Android

1

I'm doing an app and I need that in the textView it should show the commands that it sent to the outside and it also has to show the answers,

to differentiate what is interrogation and what is answers, so I need to change the color of the text depending on whether it comes out or enters the device, by default the answers are configured in the blue color xml layout, using this code I try change only the questions to red color, but there are errors and I can not achieve what I need

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    mDumpTextView = (TextView) findViewById(R.id.tv1_ReadValues);
    mTextoEditor1 = (EditText)findViewById( R.id.et1_WriteValues ) ;
    mTextoEditor2 = (EditText)findViewById( R.id.et2_WriteValues ) ;
    mBotonSend1 = (Button)findViewById( R.id.bt1_SendButton ) ;
    mBotonSend2 = (Button)findViewById( R.id.bt2_SendButton ) ;

    mBotonSend1.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mTextoEditor1.length() != 0x00){

                String bufferTXD1 = mTextoEditor1.getText().toString() + "\r" + "\n";
                mDumpTextView.append(bufferTXD1);
                mTextoEditor1.setText( "" );

            }
        }
    } );

    mBotonSend2.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mTextoEditor2.length() != 0x00){

                String bufferTXD2 = mTextoEditor2.getText().toString() + "\r" + "\n";
                mDumpTextView.append(bufferTXD2);

                nroBuffer = mDumpTextView.length();
                nroChar = bufferTXD2.length();
                starChar = nroBuffer - nroChar;
                nroChar = nroChar - 2;
                endChar = nroBuffer - nroChar;

                SpannableStringBuilder builder = new SpannableStringBuilder();

                SpannableString textColor = new SpannableString(bufferTXD2);
                textColor.setSpan(new ForegroundColorSpan(Color.RED), starChar, endChar, 0);
                builder.append(textColor);


                mTextoEditor2.setText( "" );
            }
        }
    } );

}

}

Somebody could tell me how to do it, thanks.

  

If you want to use a SpannableString, you can do it this way, defining using the red color ("# FF0000") or the blue color ("# 0000FF");

There must be some error, because the input and output text comes out of the color (blue), even I have already changed to color, red, green, yellow .. always comes out blue.

    
asked by W1ll 31.10.2018 в 19:14
source

1 answer

1

The interrogation value that would be blue can add the container <font> of html defining the blue color:

"<font color=#0000FF>" + valor + "</font>";

in the case of the response defines the color red:

"<font color=#FF0000>" + valor + "</font>";

and when displaying in your TextView the text uses the Html.fromHtml(...) method, this way the text with the desired color will be displayed.

   myTextView.setText(Html.fromHtml(valor));

If you want to use a SpannableString , you can do it this way, by defining the color red ( "#FF0000" ) or blue ( "#0000FF" );

  ... 
  SpannableStringBuilder builder = new SpannableStringBuilder(); 

  SpannableString textColor = new SpannableString(bufferTXD2);
  ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FF0000"));// Puedes usar tambien .. new ForegroundColorSpan(Color.RED);
  textColor.setSpan(colorSpan, 0, bufferTXD2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

  builder.append(textColor);

  mTextoEditor2.setText( builder );
  ...

Review this related information:

Change the color of a TextView only to certain characters

Bold in a part of a TextView

    
answered by 31.10.2018 / 19:45
source