Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 943
Vivek Kalkur Avatar asked Nov 04 '11 06:11

Vivek Kalkur


2 Answers

Try this

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height); layoutParams.gravity=Gravity.CENTER; ImageIcons[i].setLayoutParams(layoutParams); 
like image 138
Walid Hossain Avatar answered Oct 07 '22 18:10

Walid Hossain


get the layout parameters from view, modify it and set it again.

image.setBackgroundResource(R.drawable.mobile); LayoutParams params = (LayoutParams) image.getLayoutParams(); params.gravity = Gravity.CENTER; image.setLayoutParams(params); 
like image 40
Muhammad Aamir Ali Avatar answered Oct 07 '22 17:10

Muhammad Aamir Ali