'ListView with multiple elements in one row

I try to get multiple elements in one row with ListView (Like float:left in html/css) but it won't work.

How can I achieve - for example - that I have 2 or 3 elements in portrait mode and 6 or 8 in landscape. Depending on the width of the elements.

buttonlist.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    app:layout_constraintWidth="80dp"
    android:layout_weight="2"
    >
    <Button
        android:id="@+id/buttonText"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_centerInParent="false"
        android:background="@drawable/ic_button"
        android:padding="0dp"
        android:text="ButtonName"
        android:textColor="@color/black"
        android:textSize="12dp"
        android:textStyle="bold"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Float-Test"
        android:padding="20dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="@color/black"
        />


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:dividerHeight="10dp"></ListView>


</RelativeLayout>

Which point am I missing, or is it just the wrong way?

Cheers, Chriz



Solution 1:[1]

Is buttonlist.xml the layout for your list items? If so, you can just add whatever other components you need to display there however you like.

And if you want a different look and feel for different screen orientations, you need to create screen-specific layouts in appropriate directories, such as res/layout/sw-600dp. See support different screen sizes for more information.

If you're supporting different data displayed based on orientation, you'll need to make sure your adapter handles it properly as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zen_of_kermit