Recyclerview columns same width (not fixed) but take as much space as needed

I've been at this problem for over a month now. What I want is simple. I want like a table where each column takes as much space as is needed for the other columns in the other rows. So in the picture here: Example I'd like all pink columns to take as much space as the one with "Player 33333". Likewise for all other columns. But never more then is needed.

I've checked like up to 50 different posts here about people having similiar problems and most of them all they've done wrong is either not having the layout set to parent viewgroup:

    @Override
    public AdapterPlayerVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gameplayers,parent,false);
        return new AdapterPlayerVH(view);
    }

Some say you should have the container set to wrap content and child views to match_parent:

<?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"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#0000FF"
    android:orientation="horizontal">
    <TextView
        android:background="#FF00FF"
        android:id="@+id/tvwPlayerName"
        android:text="@string/stdName"
        android:textSize="@dimen/fontsizenormal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:background="#FFFF00"
        android:id="@+id/tvwPlayerChance"
        android:text="@string/stdChance"
        android:textSize="@dimen/fontsizenormal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:background="#FF0000"
        android:id="@+id/tvwPlayerFreecard"
        android:text="0"
        android:textSize="@dimen/fontsizenormal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Result: result2

Other posts tell me to use weight and 0dp like this:

<?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"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#0000FF"
    android:orientation="horizontal">
    <TextView
        android:background="#FF00FF"
        android:id="@+id/tvwPlayerName"
        android:text="@string/stdName"
        android:textSize="@dimen/fontsizenormal"
        android:padding="5dp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
    <TextView
        android:background="#FFFF00"
        android:id="@+id/tvwPlayerChance"
        android:text="@string/stdChance"
        android:textSize="@dimen/fontsizenormal"
        android:padding="5dp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
    <TextView
        android:background="#FF0000"
        android:id="@+id/tvwPlayerFreecard"
        android:text="0"
        android:textSize="@dimen/fontsizenormal"
        android:padding="5dp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
</LinearLayout>

Result: result3

I've even tried something one got to work where they set layoutparams in the onCreateViewHolder for each view.

Tried changing to constraint layout within the layout I'm inflating in the adapter.

So if anyone have any suggestions I'm all ears. I want a simple grid/table where each row has the same size on the columns and only as big as it needs, also no linebreaks on the text. Had some attempts with equal size but it clip'd the text then.

In my Activity:

rcvPlayers.setLayoutManager(new LinearLayoutManager(this));
adapterGamePlayer = new AdapterGamePlayer(this, DB.GetCursor("players"));
rcvPlayers.setAdapter(adapterGamePlayer);

In my ActivityLayout:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent">
        <TextView
            android:id="@+id/tvwGamePlayers"
            android:layout_marginStart="10dp"
            android:layout_width="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        </TextView>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rcvPlayers"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            app:layout_constrainedWidth="false"
            app:layout_constraintHeight_percent=".75"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvwGamePlayers">

        </androidx.recyclerview.widget.RecyclerView>
    </androidx.constraintlayout.widget.ConstraintLayout>

If you need more of the code just let me know.

Update

I continued some experimenting and found a "decent" solution. I ended up using a constrainedlayout in my item and the magic came from using app:layout_constraintWidth_min="wrap" together with android:layout_width="0dp". The 0dp width fills remaining space but alone it will also take 0 space. So the pink(first textview) and the yellow would take up all the space and leave 0 for the middle one at the bottom row. But with app:layout_constraintWidth_min="wrap" it also takes up atleast as much as needed the for the content. Then align them to end and it looks decent.

Result

Not how I wanted with each column same size but the next best thing.

The item:

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="#0000FF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
    
        <TextView
            android:background="#FF00FF"
            android:id="@+id/tvwPlayerName"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name:"/>
    
        <TextView
            android:background="#00FF00"
            android:id="@+id/tvwPlayerChance"
            android:text="Chance"
            android:textAlignment="textEnd"
            android:layout_marginStart="10dp"
            app:layout_constraintWidth_min="wrap"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toEndOf="@id/tvwPlayerName"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toStartOf="@id/tvwPlayerFreecard"/>
    
        <TextView
            android:background="#FFFF00"
            android:id="@+id/tvwPlayerFreecard"
            android:gravity="center"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>

The recyclerview in my Activity:

        <androidx.recyclerview.widget.RecyclerView
        android:background="#FF0000"
        android:id="@+id/rcvPlayers"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    </androidx.recyclerview.widget.RecyclerView>

Upvotes: 0

Views: 76

Answers (0)

Related Questions