Different heights in an Android GridLayout

Beril Bayram :

I'm working on creating a note app which is similar to Google Keep in its appearance. However, I couldn't figure out how to dynamically change heights of the note cards based on their text length. Here is a screenshot from my app:

app screenshot

Here is the layout file for the cards:

notes_card.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:id="@+id/notes_card"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    app:cardElevation="3dp"
    android:layout_margin="4dp"
    app:cardUseCompatPadding="true"
    android:layout_height="wrap_content"
    card_view:cardMaxElevation="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/note_text1"
            android:minLines="3"
            android:maxLines="15"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

Layout of the notes fragment:

fragment_notes_view.xml

<androidx.recyclerview.widget.RecyclerView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notes_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    />

Fragment onViewCreated function:

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        //Initialize the RecyclerView and push it onto the fragment
        RecyclerView recyclerView = view.findViewById(R.id.notes_recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(NotesView.this.getContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        NotesRecyclerAdapter adapter = new NotesRecyclerAdapter(this.getContext());
        recyclerView.setAdapter(adapter);
        super.onViewCreated(view, savedInstanceState);
    }

RecyclerViewAdapter code:

package xyz.incepted.thereminder.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import xyz.incepted.thereminder.R;
import xyz.incepted.thereminder.types.QuickNote;

public class NotesRecyclerAdapter extends RecyclerView.Adapter <NoteViewHolder> {
    //Initialize class variables
    private Context context;
    private ArrayList<QuickNote> notes;
    private NotesConnector notesConnector = new NotesConnector();

    /**
     * Creates a RecylerViewAdapter based on the context, also fetches the notes from the NotesConnector
     * @param context context to be based on
     */
    public NotesRecyclerAdapter(Context context){
        this.context = context;
        this.notes = notesConnector.getNotes();
    }

    /**
     * Executed when the RecylerView is created
     * @param parent parent of the view
     * @param viewType type of the view
     * @return
     */
    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //Inflate the view and return it
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notes_card, parent, false);
        return new NoteViewHolder(view);
    }

    /**
     * Sets the view properties
     * @param holder holder
     * @param position which text to be used
     */
    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
        holder.text1.setText(notes.get(position).getText1());
    }

    /**
     * Returns the item size of the list
     * @return item size
     */
    @Override
    public int getItemCount() {
        return notes.size();
    }
}

/**
 * Class to hold NoteView
 * //TODO divide it into two, since QuickNote and Note will have different designs
 */
class NoteViewHolder extends RecyclerView.ViewHolder{
    TextView text1;
    NoteViewHolder(View view){
        super(view);
        text1 = view.findViewById(R.id.note_text1);
    }
}
Tamir Abutbul :

how to dynamically change heights of the note cards based on their text length - you can just set the layout height to wrap_content like this:

android:layout_height="wrap_content"

You can put your items in scrollView like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="short text !!!!!!!!!!!!?!@#!@%$%$#"
        android:layout_marginBottom="8dp"/>


    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="Longggggggggggggggggggggggggggggggggggggggggggggggg  text very long"/>
</LinearLayout>

</ScrollView>

And it will look like this:

enter image description here

The blue line represents the separation between your 2 views.

If you want to achieve this using RecyclerView check out this post, Staggered RecyclerView and Heterogeneous GridLayout

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134088&siteId=1