MrPool
MrPool

Reputation: 403

Listview and TextView alignment

I have a listView which takes data from a rest API server and displays it. I need to add a textView on the side of it describing the type of data.

The right half of the screen has the listView and the left half has the textView.

The problem arises when the textView moves to accommodate the different screen sizes. I need the textView to be fixed and to be shown in conjunction with listView, and be in a straight line with the listView, regardless of the screen size. Cheers.

Edit: Image added of the sketch

Imgur link

Upvotes: 1

Views: 102

Answers (2)

Mike
Mike

Reputation: 5572

It sounds like a ListView is not what you want to use. You cannot align Views with the children of a ListView. You would need to either put the TextViews within the ListView's children or not use a ListView.

The simplest way to do this would be a vertical LinearLayout containing many horizontal LinearLayouts.

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"/>
        <Item that was in the ListView />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"/>
        <Item that was in the ListView />

    </LinearLayout>

    ...
</LinearLayout>

The ListView is really for scrolling and when you don't know how many items there will be.

Perhaps you already have an adapter in which you are handling the creation of the items and that's why you are using a ListView. You can still create the LinearLayout's children programmatically by inflating each item individually and adding it to the container view.

Original Answer:

You probably want the TextViews to scroll with the ListView as well. So I would think you should use one ListView, then add the TextView to the left side of the ListView item.

Upvotes: 1

Vishal Chhodwani
Vishal Chhodwani

Reputation: 2567

You should use weightSum, Here is an example.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="horizontal">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello World"/>

</LinearLayout>

Upvotes: 0

Related Questions