0

I have a TextView in my application. Based on the value I get from server, I want to set the text color of the TextView.

This is my textview

 <TextView
            android:id="@+id/approval_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:text="W" />
  • if P I have to set Orange
  • if A I have to set green
  • if R I have to set red

How I can do this?

Renjith
  • 5,783
  • 9
  • 31
  • 42
Pooja Dubey
  • 683
  • 2
  • 14
  • 34

7 Answers7

2

Parse the response

public enum Status { PENDING, APPROVED, REJECTED }

public static Status statusFromResponse(String response) {
    if (response == "P") {
        return PENDING;
    } else if (response == "A") {
        return APPROVED;
    } else {
        return REJECTED;
    }
}

Retrive the TextView through

TextView tv = (TextView)findViewById(R.id.approval_status);

Then choose the color

switch (valueFromServer) {
     case PENDING:
         tv.setTextColor(0xff9900); //CHOOSE YOUR ORANGE
         break;
     case APPROVED:
         tv.setTextColor(Color.GREEN);
         break;
     case REJECTED:
         tv.setTextColor(Color.RED);
         break;
}
Juangcg
  • 1,038
  • 9
  • 14
1
if(P)
{
textview.setTextColor(0xffCC00);// hex code of orange as 'Color' dont have orange color in list
}
else if(A)
{
textview.setTextColor(Color.GREEN);
}
else if(R)
{
textview.setTextColor(Color.RED);
}
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
1

When you are getting vaule from server then first set that value in TextView as per ur requirenment..

Textview t = (Textview)findViewById(R.id.approval_status);
t.setText(value);


if(value.equalsIgnoreCase("P")
 t.setTextColor(Color.parseColor("#FFA500"));
else if(value.equalsIgnoreCase("A")
 t.setTextColor(Color.GREEN);
else if(value.equalsIgnoreCase("R")
 t.setTextColor(Color.RED);
else
Piyush
  • 18,895
  • 5
  • 32
  • 63
1
Textview approval_status = (Textview)findViewById(R.id.approval_status)

if(serverValue.equals("P")
    approval_status.setTextColor(0xFF6600); // Orange 
else if(serverValue.equals("A")
    approval_status.setTextColor(Color.GREEN);
else if(serverValue.equals("R")
    approval_status.setTextColor(Color.RED);
Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
0

In the code fragement where you get the answer from the server try this:

if(insertConditionForOrangeHere)
    myTextView.setTextColor(Color.ORANGE);
else if(insertConditionForGreenHere)
    myTextView.setTextColor(Color.GREEN);
else if(insertConditionForRedHere)
    myTextView.setTextColor(Color.RED);
productioncoder
  • 4,225
  • 2
  • 39
  • 65
0
Textview tv = (Textview)findViewById(R.id.approval_status)

if(serverValue.equals("P")
    tv.setTextColor(0xFF6600); // Orange 
else if(serverValue.equals("A")
    tv.setTextColor(Color.GREEN);
else if(serverValue.equals("R")
    tv.setTextColor(Color.RED);
else
    // Handle it
CodePrimate
  • 6,646
  • 13
  • 48
  • 86
0
Textview status = (Textview)findViewById(R.id.status)

if(value.equals("P")
    approval_status.setTextColor(Color.parseColor("#ffae00")); // Orange Color Code
else if(value.equals("A")
    approval_status.setTextColor(Color.GREEN);
else if(value.equals("R")
    approval_status.setTextColor(Color.RED);
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36