-1

I want to change background color of particular textview. Need different background for each textview. I write this code, textview make but this give error

"textview.findViewById().setBackgroundColor(Color.RED);"

Textview textview;
 for (int i = 0; i < 3; i++) {
        textview = new TextView(this);
        textview .setId(i);
        textview .setTextSize(15);
        textview .setTextColor(Color.BLACK);
       }

 textview.findViewById(1).setBackgroundColor(Color.RED);

self Solution

Use Linear layout and add textview

then change

LinearLayout linear_layout;
 Textview textview;
 for (int i = 0; i < 3; i++) {
        textview = new TextView(this);
        textview .setId(i);
        textview .setTextSize(15);
        textview .setTextColor(Color.BLACK);
        linear_layout.add(textview);
       }

 linear_layout.findViewById(1).setBackgroundColor(Color.RED);
Sachin M Jain
  • 75
  • 1
  • 9
  • "textview.findViewById().setBackgroundColor(Color.RED);" give error . You can do this only if you are getting textview reference from xml . But you are creating textivew reference programatically so you cant do that. If you want to set backgroudn color then do inside the for loop textview,setBackgroundColor(Color.RED) like this – sohan shetty Dec 21 '16 at 05:47
  • @sohanshetty what do you mean. – Sachin M Jain Dec 21 '16 at 05:49
  • @sohanshetty but when I click on button and pass 1 in id so I need to change only 1st textview background color rest all as it is. – Sachin M Jain Dec 21 '16 at 05:55

5 Answers5

0

Use this code

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
 Random rand = new Random();
 for (int i = 0; i < 3; i++) {
     int r = rand.nextInt(255);
    int g = rand.nextInt(255);
    int b = rand.nextInt(255);
    int randomColor = Color.rgb(r,g,b);
       Textview textview = new TextView(this);
        textview.setId(i);
        textview.setTextSize(15);
        textview.setTextColor(Color.BLACK);
        textview.setBackgroundColor(randomColor);
       linearLayout.addView(textview);
       }
Mayur Raval
  • 3,250
  • 6
  • 34
  • 57
0

Try this:

    List<TextView> textViewList = new ArrayList<>();

    for (int i = 0; i < 3; i++) {
        TextView textview = new TextView(this);
        textview.setTextSize(15);
        textview.setTextColor(Color.BLACK);
        textViewList.add(textview);
    }

    int index = 2;

    textViewList.get(index).setBackgroundColor(Color.RED);
Vüsal
  • 2,580
  • 1
  • 12
  • 31
0

Use txtCompanyName.setBackgroundResource(R.color.white);

or

Below is snippet might help you where txtChannelName is an object of TextView

txtCompanyName.setBackgroundColor(Color.RED);

or

txtCompanyName.setBackgroundColor(Color.parseColor("#ffffff"));

Hope it will help you

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
0

Create List of type TextView and add your textview like below

List<TextView> textViewList = new ArrayList<>();

for (int i = 0; i < 3; i++) {
    TextView textview = new TextView(this);
    textview.setTextSize(15);
    textview.setTextColor(Color.BLACK);
    textViewList.add(textview);
}

Then on button click pass your value to change the text background color like below

   private void changeBackGroundColor(int index) {
       textViewList.get(index).setBackgroundColor(Color.RED);
   }

This will change your respective text view bacground color

sohan shetty
  • 289
  • 1
  • 16
0

Use getRandomColor method

public int getRandomColor(){
   Random rnd = new Random();
   return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}

textViewList.get(index).setBackgroundColor(getRandomColor());

Best Solution

To Genrate Light colors use this -

 public int generateRandomLightColor() {
    // This is the base color which will be mixed with the generated one
    final int baseColor = Color.WHITE;

    final int baseRed = Color.red(baseColor);
    final int baseGreen = Color.green(baseColor);
    final int baseBlue = Color.blue(baseColor);

    final int red = (baseRed + mRandom.nextInt(256)) / 2;
    final int green = (baseGreen + mRandom.nextInt(256)) / 2;
    final int blue = (baseBlue + mRandom.nextInt(256)) / 2;

    return Color.rgb(red, green, blue);
}

It show color like - https://stackoverflow.com/a/43235/4741746

To genrate Dark Color use this -

public int getRandomDarkColor(){
        Random rnd = new Random();
        //use rnd.nextInt(0x1000000) & 0x7F7F7F
        return Color.argb(255, rnd.nextInt(0x1000000), rnd.nextInt(0x1000000), rnd.nextInt(0x1000000));
    }
Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55