Android image view gravity
Asked Answered
A

5

15

How can I dynamically set the gravity ?

Bitmap bmp;
im = new ImageView (this);
bmp=getbmp(img);
im.setImageBitmap(bmp);  

Now I would like to put the image in the top . Ho can I do this without android:gravity in main.xml ?

EDIT :

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical"

    >
</ScrollView>

I have a frame layout and I add to it the image , and then the text and I would like the image to be on the top of the screen.

FrameLayout fm = new FrameLayout (this);
fm.addView(im);
fm.addView(fin); // fin = the text view
setContentView(fm);
Abduce answered 3/10, 2011 at 7:9 Comment(5)
More info, please. Post your layout file.Replevy
Is there any reason why you don't just put the ImageView in the layout file?Replevy
Well , for one If I put the image View in the layout file , inside the ScrollView , the application crashes , reason , the selected child already has a parent . Secondly , I have a text view , and an image view , so I would like to create them dynamically .Abduce
Yeah, you're supposed to put the ImageView inside the child of the ScrollView. A ScrollView can only have one child - i.e. a LinearLayout or RelativeLayout. It shouldn't be a problem.Replevy
OK so you say to create a LinearLayout inside the ScrollView and inside the LinearLayout to put the text and image views ?Abduce
A
34

Use ImageView.setScaleType() with ImageView.ScaleType.FIT_START as value.

Abel answered 3/10, 2011 at 7:15 Comment(3)
Sorry about that, it is like this: im.setScaleType(ImageView.ScaleType.FIT_START);Abel
Thanks , it works but why does it put the text over the image ? What can I do ?Abduce
the code : FrameLayout fm = new FrameLayout (this); fm.addView(im); fm.addView(fin); setContentView(fm); fin is the textViewAbduce
W
0

You can put ImageView inside a LinearLayout and the use setLayoutParams of the LinearLayout to set gravity of the layout.

Week answered 3/10, 2011 at 7:21 Comment(0)
B
0

You can try this code if may be help you xml file

<GridView android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/gallerylistactivity_gallery"
    android:columnWidth="90dp" android:numColumns="4"
    android:verticalSpacing="5dp" android:horizontalSpacing="10dp"
    android:gravity="center" android:stretchMode="spacingWidth"/>

.java file

public class GalleryListAdapter extends BaseAdapter{

private Context mContext;

private Integer[] mImageIds = { R.drawable.gallery_01,
        R.drawable.gallery_02, R.drawable.gallery_03,
        R.drawable.gallery_04, R.drawable.gallery_05,
        R.drawable.gallery_06, R.drawable.gallery_07,
        R.drawable.gallery_08, R.drawable.gallery_10,
        R.drawable.gallery_09 };

public GalleryListAdapter(Context c) {
    this.mContext = c;

}

@Override
public int getCount() {
    return mImageIds.length;

}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mImageIds[position]);
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(3, 3, 3, 3);

    return imageView;

}
Brit answered 3/10, 2011 at 7:26 Comment(0)
A
0

See "http://developer.android.com/reference/android/widget/ImageView.ScaleType.html".

Abbevillian answered 3/10, 2011 at 7:38 Comment(0)
J
0

Try this:

Bitmap bmp;    
ImageView img= new ImageView (this);       
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.icon);      
img.setImageBitmap(bmp);             
img.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,                       
                  LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
mLinearLayout.addView(img);

Please note that you need to set width and height of imageview in FrameLayout.LayoutParams(width,height,gravity) according to what gravity you want to set.

Jealous answered 3/10, 2011 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.