Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 628
AZ_ Avatar asked Oct 10 '11 08:10

AZ_


2 Answers

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.

like image 124
Janusz Avatar answered Sep 30 '22 16:09

Janusz


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.

like image 41
AZ_ Avatar answered Sep 30 '22 16:09

AZ_