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

7

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

Ataractic answered 4/11, 2011 at 6:15 Comment(1)
Check out my answer here. It helped my centralize Imageview.Evonevonne
T
106

Try this

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height);
layoutParams.gravity=Gravity.CENTER;
ImageIcons[i].setLayoutParams(layoutParams);
Tabber answered 4/11, 2011 at 6:24 Comment(0)
F
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);
Faience answered 2/12, 2014 at 14:16 Comment(0)
A
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);
Analysis answered 4/11, 2011 at 6:40 Comment(1)
Ok got that.Was wondering what went wrong.Thanks to Android Killer.Ataractic
G
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);
Gummous answered 8/5, 2016 at 5:57 Comment(0)
D
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);
Demerol answered 26/11, 2016 at 5:34 Comment(0)
S
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);
Swindle answered 15/3, 2019 at 7:15 Comment(0)
T
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);
}
Tewfik answered 16/3, 2019 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.