45

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

Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40
  • Check out my answer [here](http://stackoverflow.com/questions/6101874/android-center-an-image/33039444#33039444). It helped my centralize Imageview. – Sindri Þór Oct 09 '15 at 13:25

7 Answers7

106

Try this

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height);
layoutParams.gravity=Gravity.CENTER;
ImageIcons[i].setLayoutParams(layoutParams);
Walid Hossain
  • 2,724
  • 2
  • 28
  • 39
12

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);
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
4

First made the width to match_parent and then set the gravity else gravity will not work.Hope it will work.

ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ImageIcons.setGravity(Gravity.CENTER);
smartDonkey
  • 550
  • 1
  • 5
  • 14
Android Killer
  • 18,174
  • 13
  • 67
  • 90
3
ImageView myImage = new ImageView(this);
FrameLayout.LayoutParams myImageLayout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
myImageLayout.gravity=Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
            myImage.setLayoutParams(myImageLayout);
0
    LinearLayout llBasicInfo = new LinearLayout(context);
    llBasicInfo.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    llBasicInfo.setOrientation(LinearLayout.VERTICAL);
    ImageView imageView = new ImageView(context);
    LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(200,200);
    layoutParams.gravity=Gravity.CENTER;
    imageView.setLayoutParams(layoutParams);
    llImage.addView(imageView);
Nilesh Savaliya
  • 666
  • 9
  • 9
0

I think the code below possible helps someone

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = 10;
layoutParams.rightMargin = 10;
parent.addView(tips[i], layoutParams);
0

if your ImageView is child of RelativeLayout

Then This May Work Helps..

public void setLogoPosition(String pos) 
{

    //_Watermark is ImageView Object

    RelativeLayout.LayoutParams layoutParams =
            (RelativeLayout.LayoutParams) _Watermark.getLayoutParams();


    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);

    switch (pos) {
        case "topleft":
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            break;
        case "topright":
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            break;
        case "bottomleft":
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            break;
        case "bottomright":
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            break;
        case "center":
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            break;
        case "topcenter":
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            break;
        case "bottomcenter":
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            break;
    }
    _Watermark.setLayoutParams(layoutParams);
}
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65