Home » Android » Android align image in top and center in RelativeLayout

Android align image in top and center in RelativeLayout

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to align image to top of my Relative Layout and after that center it horizontally. Here is the code I’m using for :

<ImageView  
        android:id="@+id/collection_image_background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"  />

,but this code is not working. My image is centered,but not aligned at top of my Relative Layout. I tried with android:scaleType="fitStart", but it’s actually align imag left and top of it’s parent.

So any idea how I can align the image correctly as I need?

P.S. I forget to mention that I’m setting the image to my ImageView like this :

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inTempStorage = new byte[8*1024];

    ops = BitmapFactory.decodeStream(cis,null,o2);
    ImageView view = (ImageView) findViewById(R.id.collection_image_background);
    view.setImageBitmap(ops);
How to&Answers:

code seems to be perfect except content area you had assign fill_parent takes whole view and will appear as center, so just modify it by wrap_content

  <ImageView  
            android:id="@+id/collection_image_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"  />