Hi All,

I have a Table layout defined in my activity XML. Via code I add rows and 
buttons to this table layout.
When I click on the button I wish to change the background color of the 
button, but it is not happening.

The logic I have to update the background color works if I use Gridview in 
conjunction with ButtonAdapter (which I wrote using ImageAdapter from 
Android website).

Questions:
1. Can someone please advice me what the error is in the following code (in 
the onClick function when I try to update the button background color)?
2. Is it better to use GridView to show rows of buttons or table layout? If 
there is any advantage over using one to the other it would be worth 
knowing.

My code is as follow:

TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
table.removeAllViews();
for (int row = 1; row <= some_row_counter; row++)
{
    TableRow tr = new TableRow(this);
    for (int column = 1; column <= some_column_counter; column++)
    {
        final Button btn = new Button(this);
        btn.setText(some_array[index].btnTxt);
        btn.setId(some_array[index].btnId);
        btn.setBackgroundColor(Color.RED);
        btn.setOnClickListener(new OnClickListener()
        {
            private boolean isChecked = false;
            @Override
            public void onClick(View v)
            {
                isChecked = !isChecked;
            
                Drawable d = v.getBackground();
          int backgroundColor = (isChecked ? Color.BLUE : Color.RED);
            PorterDuffColorFilter filter = new 
PorterDuffColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);  
            d.setColorFilter(filter);
            d.invalidateSelf();
            }
        }); 
        tr.addView(btn);
        // break if index reaches some_array max size
    }
    table.addView(tr);
    // break if index reaches some_array max size
}


Thanks for your time.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to