Android image view gravity

How can I dynamically set the gravity ?

Bitmap bmp;
im = new ImageView (this);

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

  • Fragment MyFragment not attached to Activity
  • Call transfer from one device to another through Bluetooth
  • What's the difference between Key store password and Key password in Android Sign Certification?
  • OnItemLongClickListener is not working in a custom listview
  • Adb install failure: INSTALL_CANCELED_BY_USER
  • Volley - http request in blocking way
  • EDIT :


    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android=""
        android:layout_width="fill_parent" android:layout_height="fill_parent"

    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(fin); // fin = the text view

    Related posts:

    How to solve showStatusIcon on inactive InputConnection
    Mobile Website Facebook Login using Facebook App for login details
    SearchView expand/collapse animation in Android
    Cross compiling static C hello world for Android using arm-linux-gnueabi-gcc
    Crosswalk error on android build in cordova project
    ImageWriter alternative for android 19-22?
  • Is this explanation about VSS/RSS/PSS/USS accurate?
  • View method information in Android Studio
  • Disabling all child views inside the layout
  • Parent bottom views hide with parent center layout view on opening keyboard
  • UnsupportedMethodException Android Studio 2.2
  • Emulated Android device does not re-sync time/date after restoring snapshot
  • 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;
    public int getCount() {
        return mImageIds.length;
    public Object getItem(int position) {
        return position;
    public long getItemId(int position) {
        return position;
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
        imageView.setPadding(3, 3, 3, 3);
        return imageView;

    See “”.

    Try this:

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

    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.