1
votes

I have Imageview with height and width is match_parent. I need image in horizontal center without change width to wrap_content. You can change parent layout.

Here is my xml.

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



  <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
2
just add android:layout_centerHorizontal="true" property to <ImageViewYashdeep Patel

2 Answers

1
votes
<ImageView    android:id="@+id/imageView1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:scaleType="fitCenter"
   android:src="@drawable/ic_launcher" />

This should suite your need, If you need more info please look at : scaletype description

Try the below code:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
1
votes

You should try it with android:scaleType="fitCenter". Rest depends on what your exact need is and what is the size of images you are displaying.

For More Info

Edit

If you want to center only the image in ImageView to horizontal center then add the following,

android:layout_centerHorizontal="true"

You should have the height to be wrap_content

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"
    android:src="@drawable/ic_launcher" />