please tell me how to set textview background color when textview id is dynamic

278 views Asked by At

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);
5

There are 5 answers

0
Sushant Gosavi On

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));
    }
0
Mayur Raval On

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);
       }
0
Vüsal On

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);
0
InsaneCat On

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

0
sohan shetty On

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