Vivek Kalkur Vivek Kalkur - 12 days ago 5
Android Question

Setting of ImageView's gravity to the center in android programmatically

I want to set the gravity of an array of Imageviews,ImageIcons[i] to the center with the following code,

ImageIcons[i] = new ImageView(this);
ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layout.addView(ImageIcons[i]);


And I am stuck up while setting the gravity.I request the SO people to guide me on this.

Thanks

Answer

Try this

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height);
layoutParams.gravity=Gravity.CENTER;
ImageIcons[i].setLayoutParams(layoutParams);