NoToast
NoToast

Reputation: 98

ImageView does not show up when added to a TableRow programmatically

Here are the relevant code bits:

<HorizontalScrollView
        android:id="@+id/cards_scrollview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:fillViewport="false" >

        <TableRow
            android:id="@+id/cards_container"
            android:layout_width="wrap_content"
            android:layout_height="100dp" >

            <ImageView
                android:id="@+id/test_card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="@dimen/cards_distance"
                android:adjustViewBounds="true"
                android:src="@drawable/card_back" />

        </TableRow>

</HorizontalScrollView>

and

private void addCard(Drawable d) {
        TableRow container = (TableRow) findViewById(R.id.cards_container);
        ImageView card = new ImageView(this);

        card.setAdjustViewBounds(true);
        card.setPadding(R.dimen.zero, R.dimen.zero, R.dimen.cards_distance, R.dimen.zero);
        TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        card.setLayoutParams(params);
        card.setImageDrawable(d);
        card.setVisibility(View.VISIBLE);

        container.addView(card);
}

I'm positively sure the Drawable I'm trying to add is not null, has the bounds properly set, the alpha set to 255, and fits properly. I've tested this by applying it to test_card instead of trying to add new ImageViews, which works perfectly.

Is there anything I'm missing here?

Upvotes: 1

Views: 540

Answers (1)

NoToast
NoToast

Reputation: 98

After an hour of tinkering with the code, it turns out setPadding() actually takes ints and not resource references as arguments, and that's what was messing it all up.

So basically all I needed to do was change:

card.setPadding(R.dimen.zero, R.dimen.zero, R.dimen.cards_distance, R.dimen.zero);

to

card.setPadding(0, 0, 5, 0);

Upvotes: 1

Related Questions