Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center ImageView in Relative Layout

I have an image and a textview inside a relative layout. The image and the textview take up 50% of the screen width. How can I center the image and the textview ? I have tried many methods and I don't know what I'm doing wrong...

Here is my code:

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textViewEventImage"
                android:adjustViewBounds="true" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/eventNameBackground"
                android:id="@+id/textViewEventName"
                android:textColor="@android:color/white"
                android:textSize="15sp"/>
        </RelativeLayout>
    </LinearLayout>

enter image description here

like image 269
Jak111 Avatar asked Jan 14 '17 17:01

Jak111


3 Answers

Try this code it will suits your requirement

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#ff0000" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="180dp"
        android:layout_height="120dp"
        android:layout_centerInParent="true"
        android:gravity="center_vertical"
        android:src="@drawable/americanexpress" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:text="Your product" />
</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:background="#00ff00" />

</LinearLayout>
like image 82
Rajakumar Avatar answered Sep 18 '22 11:09

Rajakumar


Try like this

Put imageview as

centerinparent ="true"

And textview also centerinparent="true"

And add one attribute to textview As

android: layout_below="@+id/imageview id"
like image 34
maulik Avatar answered Sep 21 '22 11:09

maulik


May be this will help

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    <ImageView
        android:layout_width="xxxdp"
        android:layout_height="xxxdp"
        android:src="@drawable/someimage"
        />
    <TextView
        android:layout_width="xxxdp"
        android:layout_height="xxxdp" 
        android:text="fweqf"/>
    </LinearLayout>

</RelativeLayout>
like image 23
Simo Avatar answered Sep 19 '22 11:09

Simo