tamrezh21
tamrezh21

Reputation: 125

How to set a layout in the middle of thescreen to show Image and text Android

I have some images from url data, which I want to show in a detail view of the app. To do that Ihave set a Liner Layout, and imageView inside the layout and the text for title. Bit the problem is the image is showinf at the top of the layout, not in the cenrer of the screen. How can I set the image in the middle of that layer and text below the image. Here is my XML code for that layout

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

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

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:text="Hallo world" />

</LinearLayout>

Upvotes: 0

Views: 62

Answers (2)

J Ramesh
J Ramesh

Reputation: 548

Update xml file like this


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/detail_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
                android:scaleType="fitCenter" />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Hallo world" />
    </LinearLayout>
    </LinearLayout>

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69709

try this use RelativeLayout insted of LinearLayout and

make your ImageView android:layout_centerInParent="true"

make your TextView android:layout_below="@id/detail_image" like below code

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

    <ImageView
        android:id="@+id/detail_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_centerInParent="true"
        android:scaleType="fitCenter" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:layout_below="@id/detail_image"
        android:text="Hallo world" />

</RelativeLayout>

Upvotes: 1

Related Questions