W I Z A R D
W I Z A R D

Reputation: 1224

Android: Set textcolor for programmatically created TextView

I have created TextView programmatically, Now i want to set text color to the TextView below is my code

TableLayout ll = (TableLayout) findViewById(R.id.auditContent);
public TableRow row;
TextView txtNumber;

for (int i = 0; i < ItemCount; i++) {
row = new TableRow(MainActivity.this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
                    row.setLayoutParams(lp);
                    row.setWeightSum(1f);
      txtNumber = new TextView(MainActivity.this);
      txtNumber.setGravity(Gravity.CENTER);
      txtNumber.setText("No." + count);
      txtNumber.setTextColor(getResources().getColor(R.color.blue)); //setting text color

 row.addView(txtNumber);

ll.addView(row, i);
    }

The textcolor is not setting the color to TextView text, m doing anything wrong, And i debug the code there is no error. Please help thanks

In string.xml <color name="blue">#33CCCC</color> m not using color.xml The above color will work fine for xml TextView

Upvotes: 5

Views: 29052

Answers (7)

Sharanjeet Kaur
Sharanjeet Kaur

Reputation: 826

tv_name.setTextColor(Color.parseColor("#bdbdbd"));

Upvotes: 1

Vikram Kodag
Vikram Kodag

Reputation: 574

Use this function to set TextView color programmatically

private void setViewColor(TextView inputTextView, int colorId) {
    //From API 23, getResources().getColor() is deprecated
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        inputTextView.setTextColor(ContextCompat.getColor(context, colorId));
    } else {
        inputTextView.setTextColor(context.getResources().getColor(colorId));
    }
}

Upvotes: 0

Parth Vora
Parth Vora

Reputation: 1718

Starting from Android Support Library 23

txtNumber.setTextColor(ContextCompat.getColor(context, R.color.your_color));

Upvotes: 6

Pankaj Arora
Pankaj Arora

Reputation: 10274

//define global

int color;

// in on create

color = Integer.parseInt("YOUR COLOR CODE", 16)+0xFF000000;
{
 txtNumber = new TextView(MainActivity.this);
 txtNumber.setGravity(Gravity.CENTER);
 txtNumber.setTextColor(color); //setting text color
}

Upvotes: 0

Naval Sharma
Naval Sharma

Reputation: 1

Use this to change text color :

textview.setTextColor(new Color().parseColor("#ffffff"));

Upvotes: -2

Piyush
Piyush

Reputation: 18933

According to your xml file you need to change

txtNumber.setTextColor(getResources().getColor(R.color.blue));

to

txtNumber.setTextColor(getResources().getString(R.color.blue));

Further more you can make color.xml file in your values folder and in that use

<color name="mycolor">#33CCCC</color>

now just use this way

txtNumber.setTextColor(getResources().getColor(R.color.mycolor));

Upvotes: 11

Shriram
Shriram

Reputation: 4411

USe

text.setTextColor(Color.rgb(200,0,0));
setTextColor(Color.parseColor("#FFFFFF"));
text.setTexColor(getResources().getColor()(R.color.colorname)

make sure your resources will be

#eaeaea

Upvotes: 0

Related Questions