0

hi i made a program that when i input number in my editText it will automatically display tablerow that contains edittexts equal to the number that i inputted, i made use of for loop to do this, but i want that in every loop it will change the background color of the edittext how will i do that, thanks in advance...

here are my codes...

final EditText  et = (EditText)findViewById(R.id.questions);
    Button button1 = (Button)findViewById(R.id.btn);
    button1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String c = et.getText().toString();
            Integer count = Integer.parseInt(c);


            TableLayout table = new TableLayout(getApplicationContext());
            table.setVerticalScrollBarEnabled(true);
            table.setPadding(10, 10, 10, 10);


            TableRow tableRow = new TableRow (getApplicationContext());
            TextView txt = new TextView (getApplicationContext());
            TextView txt2 = new TextView (getApplicationContext());
            TextView txt3 = new TextView (getApplicationContext());
            TextView txt4 = new TextView (getApplicationContext());
            TextView txt5 = new TextView (getApplicationContext());
            TextView txt6 = new TextView (getApplicationContext());
            tableRow.addView(txt);
            tableRow.addView(txt2);
            tableRow.addView(txt3);
            tableRow.addView(txt4);
            tableRow.addView(txt5);
            tableRow.addView(txt6);
            txt.setText("Question  ");
            txt2.setText("Excellent   ");
            txt3.setText("Best     ");
            txt4.setText("Better   ");
            txt5.setText("Good     ");
            txt6.setText("Poor     ");

            table.addView(tableRow);

            int j=0;
            for(j = 1; j<=count; j++){

                tableRow = new TableRow (getApplicationContext());
                TextView name = new TextView (getApplicationContext());
                EditText et2 = new EditText (getApplicationContext());
                EditText et3 = new EditText (getApplicationContext());
                EditText et4 = new EditText (getApplicationContext());
                EditText et5 = new EditText (getApplicationContext());
                EditText et6 = new EditText (getApplicationContext());

                et2.setBackgroundColor(Color.WHITE);
                et3.setBackgroundColor(Color.WHITE);
                et4.setBackgroundColor(Color.WHITE);
                et5.setBackgroundColor(Color.WHITE);
                et6.setBackgroundColor(Color.WHITE);





                tableRow.addView(name);
                tableRow.addView(et2);
                tableRow.addView(et3);
                tableRow.addView(et4);
                tableRow.addView(et5);
                tableRow.addView(et6);
                table.addView(tableRow);

            }
            setContentView(table);



        }

    });
Dio Baccay
  • 83
  • 8

3 Answers3

1

Try this

for(j = 1; j<=count; j++){

                tableRow = new TableRow (getApplicationContext());
                TextView name = new TextView (getApplicationContext());
                EditText et2 = new EditText (getApplicationContext());
                EditText et3 = new EditText (getApplicationContext());
                EditText et4 = new EditText (getApplicationContext());
                EditText et5 = new EditText (getApplicationContext());
                EditText et6 = new EditText (getApplicationContext());

                Random rnd = new Random(); 
                int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 

                et2.setBackgroundColor(color);
                et3.setBackgroundColor(color);
                et4.setBackgroundColor(color);
                et5.setBackgroundColor(color);
                et6.setBackgroundColor(color);





                tableRow.addView(name);
                tableRow.addView(et2);
                tableRow.addView(et3);
                tableRow.addView(et4);
                tableRow.addView(et5);
                tableRow.addView(et6);
                table.addView(tableRow);

            }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0

You give random color function and return from function one color...

Android: Generate random color on click?

try this one.

Community
  • 1
  • 1
AndyBoy
  • 564
  • 6
  • 29
0

To add Random background drawable colors:

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
View.setBackgoundDrawable(new ColorDrawable(color)));
amalBit
  • 12,041
  • 6
  • 77
  • 94