AZ_
AZ_

Reputation: 21899

android ImageView setPadding has no effect

I am trying to set padding of an ImageView. My code is below

private void createEpisodeView() {
    float scale = this.getResources().getDisplayMetrics().density;
    int padding = (int) (PADDING * scale + 0.5f);

    rlItemsRoot = (LinearLayout) findViewById(R.id.rl_items_root);

    for (int i = 0; i < GameLevels.TOTAL_EPISODES; i++) {
        ImageView iv = new ImageView(this);

        iv.setPadding(padding, padding, padding, padding);          
        iv.setBackgroundResource(R.drawable.icon_small);

        rlItemsRoot.addView(iv);
    }

}

But it has no effect. but when I set this in XML it looks fine.

Upvotes: 6

Views: 5253

Answers (2)

AZ_
AZ_

Reputation: 21899

Rather than setting

iv.setBackgroundResource(R.drawable.icon_small);

set

iv.setImageResource(R.drawable.icon_small);

and problem solved :)

If any body knows the reason please reply.

Upvotes: 4

Janusz
Janusz

Reputation: 189484

A you noticed yourself you are using

iv.setBackgroundResource(R.drawable.icon_small);

This will set the Background for the ImageView. The Background Image will fill the whole view because it is behind all the content in the View.

Use

iv.setImageResource(R.drawable.icon_small);

instead.

Upvotes: 13

Related Questions