Marek Zima
Marek Zima

Reputation: 5

Center image in android viewpager

I have the ViewPager in the MainActivity, in every page is RelativeLayout, in the RelativeLayout is ImageView and TextView, I want the image and text in the center of the page.

Something like this not working

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

When I try a device with a really small size of the screen, the image with the text are in the bottom.

ViewPager:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/abs__bright_foreground_disabled_holo_dark">
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_marginTop="120dp"
    android:layout_marginBottom="120dp"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:layout_weight="1">
</android.support.v4.view.ViewPager></RelativeLayout>

RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/hoo">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageSubject"
    android:layout_alignParentTop="true"
    android:layout_centerInParent="true"
    android:layout_marginTop="211dp"
    android:src="@drawable/abs__ab_bottom_solid_dark_holo"
    android:onClick="onClickSubject" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text"
    android:layout_below="@+id/imageSubject"
    android:layout_centerInParent="true"
    android:layout_marginTop="28dp" /></RelativeLayout>

Upvotes: 0

Views: 2980

Answers (1)

lopez.mikhael
lopez.mikhael

Reputation: 10071

You can use CENTER_IN_PARENT for relative layout.

Add android:layout_centerInParent="true" to element which you want to center in the RelativeLayout

In your layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hoo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageSubject"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClickSubject"
            android:src="@drawable/abs__ab_bottom_solid_dark_holo" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

Result in image :

enter image description here

Upvotes: 3

Related Questions