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);
Teo
  • 3,394
  • 11
  • 43
  • 73
  • More info, please. Post your layout file. – Michell Bak Oct 03 '11 at 07:13
  • Is there any reason why you don't just put the ImageView in the layout file? – Michell Bak Oct 03 '11 at 07:24
  • 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 . – Teo Oct 03 '11 at 07:30
  • 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. – Michell Bak Oct 03 '11 at 07:32
  • OK so you say to create a LinearLayout inside the ScrollView and inside the LinearLayout to put the text and image views ? – Teo Oct 03 '11 at 07:33

5 Answers5

34

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

JJD
  • 50,076
  • 60
  • 203
  • 339
Mahdi Hijazi
  • 4,424
  • 3
  • 24
  • 29
  • Sorry about that, it is like this: im.setScaleType(ImageView.ScaleType.FIT_START); – Mahdi Hijazi Oct 03 '11 at 07:22
  • Thanks , it works but why does it put the text over the image ? What can I do ? – Teo Oct 03 '11 at 07:28
  • the code : `FrameLayout fm = new FrameLayout (this); fm.addView(im); fm.addView(fin); setContentView(fm);` `fin` is the `textView` – Teo Oct 03 '11 at 07:28
0

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

Hemant Sharma
  • 249
  • 1
  • 2
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;

}
Girish Patel
  • 1,270
  • 4
  • 16
  • 30
0

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

Bai
  • 135
  • 1
  • 2
  • 8
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.

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56