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

Vivek Kalkur picture Vivek Kalkur · Nov 4, 2011 · Viewed 69.3k times · Source

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

Walid Hossain picture Walid Hossain · Nov 4, 2011

Try this

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