Android image view gravity

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 ?

  • Android unit testing with Junit: testing network/bluetooth resources
  • Elevation not working for BottomSheet inside Coordinator layout
  • Parcelable inheritance: abstract class - Which CREATOR?
  • DownloadManager COLUMN_LOCAL_FILENAME deprecated
  • Does any one know the 'top' command 's result's meaning in android?
  • How can I script genymotion emulator to launch a given avd, headless?
  • 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);
    

    Related posts:

    How can I set my shape&#039;s background in xml?
    PhoneGap/Android - Locking orientation to portrait
    Creating categories in a ListView?
    onClick event is not triggering | Android
    Change source image for image view when pressed
    facebook, android java.lang.IllegalStateException: Cannot execute task: the task is already running
  • google chrome animation effect in android
  • Adding Fragments Dynamically
  • Changing the text color
  • Passing JSONObject into another activity
  • Android's activity lifecycle after coming out of sleep
  • Setting WebView to view Desktop Site and Not Mobile Site
  • 5 Solutions collect form web for “Android image view gravity”

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

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

    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;
    
    }
    

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

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.