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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With