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.