0

I have an intent that brings two strings. 1 is the string for the message and the other is a string for the color.

The code below shows that the 2 strings are received. The "message" string is displayed in the textview which works correctly. However I need the string "messagecolor" to set the color of the textview.

I have a colors.xml file where the colors blue, green and red are defined.

So if the string "messagecolor" is blue to text will be blue. The same for red and green. If the string "messagecolor is not blue,red or green then the text will just appear black.

If someone could help me solve this it would be most appreciated.

Thank you

// Get the message from the intent
    Bundle bundle = getIntent().getExtras();
    String messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR);
    String message = bundle.getString(MainActivity.EXTRA_MESSAGE);


    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextColor(getResources().getColor(R.color.blue));
    textView.setTextSize(100);
    textView.setText(message);

3 Answers 3

3

Instead of trying to match which colors are represented by which string, why don't you pass the resource ID itself?

Sign up to request clarification or add additional context in comments.

Comments

0

Try

if (message.equals("messageBlue")) {
    textView.setTextColor(getResources().getColor(R.color.blue));
} else if (message.equals("messageGreen")) {
    textView.setTextColor(getResources().getColor(R.color.green));
} else if (message.equals("messageRed")) {
    textView.setTextColor(getResources().getColor(R.color.red));
} else //default
    textView.setTextColor(getResources().getColor(R.color.black));

Comments

0
text.setText(Html.fromHtml("<font color='red'>R</font>"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.