androidandroid-layoutlistviewlistview-adapter

Centering a layout in a ListView


I dont get it - I cannot align the RelativeLayout in the ListView - that is I want to center the RelativeLayout that holds and image and some textviews.

The RelativeLayout is still on the lefthand-side of the screen, though the listview-width should match the parent. What could be wrong?

Here's the xml, for the list item layout in the adapter.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout_os_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">

    <ImageView
        android:id="@+id/os_listview_imageview1_id"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_margin="10dp"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher_background"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/os_listview_imageview1_id"
        android:orientation="vertical"
        android:layout_centerVertical="true">

    <TextView
        android:id="@+id/os_listview_name_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nougat"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/os_listview_version_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:text="7.0"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/os_listview_api_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="24"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/os_listview_release_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="2016"
        android:layout_centerVertical="true"/>
    </LinearLayout>
</RelativeLayout>

enter image description here

And the ListView is here in the following layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    tools:context="se.listview.stude.listviewtest180303.MainActivity">

    <ListView
        android:id="@+id/os_listView_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Edit: this is how it looks the list item in the design-mode in android-studio.

enter image description here


Solution

  • Use this as rootLayout of your item :-

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

    You would get the same output..

    Also in your activity put listView width as match_parent