I am making an app and I need the textView to show the at commands that it sent to the outside and also have to show the responses,
to differentiate which is a question and which is an answer, that's why I need to change the color of the text depending on whether it leaves or enters the device, by default the answers are configured in the xml layout to blue, using this code I try to change only the questions in red, but there are errors and I can't get 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( "" );
}
}
} );
}
}
Could someone tell me how to do it, thanks.
If you want to use a SpannableString, you can do it this way, defining to use the color red ("#FF0000") or blue ("#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... it always comes out blue.
The question mark value that would be blue can be added to the
<font>
html container by defining the blue color:in the case of the response defines the color red:
and when displaying in your
TextView
the text use the methodHtml.fromHtml(...)
, this way the text will be displayed with the desired color.If you want to use a SpannableString , you can do it this way, defining to use the color red (
"#FF0000"
) or blue ("#0000FF"
) ;Check out this related information:
Change the color of a TextView only to certain characters
Bold in a part of a TextView