Capiono
Capiono

Reputation: 220

adding ImageView to tableLayout programmatically

I have a list of bitmap images, How can I add this bitmap to a tableLayout programmatically?

thanks.

Upvotes: 0

Views: 7170

Answers (1)

pjama
pjama

Reputation: 3044

Try something like this:

    TableLayout table = new TableLayout(this);
    for (int i = 0; i < mRows; i++) {

        TableRow tr = new TableRow(mContext);
        tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        for (int j = 0; j < mCols; j++) {

            ImageView view = new ImageView(this);
            view.setImageResource(R.drawable.star_on)
            tr.addView(view);
        }
        table.addView(tr);
    }

This generates the whole table / grid programmatically. You can see how it instantiates an ImageView and sets the image.

Upvotes: 6

Related Questions