1

I'm trying to vertically center align ImageView in RelativeLayout but unable to do so. It seems a simple thing but I'm unable to sort out the problem. Here is the xml I defined.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="63dp"
    android:background="#aa00ff">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="12dp"
        android:layout_marginLeft="12dp"
        android:background="#ffff00"/>


</RelativeLayout>

NOTE: This view is being used for ListView row.

enter image description here

Sathish
  • 33
  • 4
Ammar
  • 1,811
  • 5
  • 26
  • 60
  • I just tested your code and it works - The ImageView is indeed vertically centered. Can you post the Activity's code? Perhaps you are doing something there that overrides the Layout params from the XML. – nitzanj May 22 '14 at 07:18
  • @Ammar increase the height of relative layout. i got correct view as in image shown – Top Cat May 22 '14 at 07:27
  • @nitzanj, @Lollipop! This layout is causing problem when used for list view row. If you try it out of activity, it works perfectly fine. Strange behavior...! – Ammar May 22 '14 at 07:56

5 Answers5

5

Remove this

android:layout_alignParentLeft="true"

and try this

android:layout_centerInParent="true"

to your ImageView

M D
  • 47,665
  • 9
  • 93
  • 114
2

Since it's a listView you're talking about (next time mention it from the start please...) it changes the problem. Do you use a custom adapter?

If so in your getView when you inflate the xml you should do it like so:

view = LayoutInflater.from(context).inflate(R.layout.item_layout, viewGroup, false);

This makes sure that your layout params are kept - If you give it null as the parent the params are completely ignored.

nitzanj
  • 1,699
  • 1
  • 10
  • 14
0

try this,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="63dp"
    android:background="#aa00ff">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:background="#ffff00"/>


</RelativeLayout>
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0
Try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#aa00ff">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:background="#ffff00"/>


</RelativeLayout>
Top Cat
  • 2,473
  • 3
  • 22
  • 34
0
// Try this way,hope this will help you to solve your problem...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="63dp"
    android:background="#aa00ff"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_marginRight="12dp"
        android:layout_marginLeft="12dp"
        android:background="#ffff00"/>


</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67