PeterPan
PeterPan

Reputation: 744

Change the size of the first GridView column

I'm trying to achieve that the first column of my GridView is a bit wider than the other four:

enter image description here

As you can see in the screenshot, the first column is not wide enough to display the whole date without a line break. Since there's some space left in the other four columns, I'd like to shrink them and give the first column a bit more width.

At the moment, I'm using an ArrayAdapter with a simple_list_item_1 und achieve my current result.

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
testView.setAdapter(adapter);

My XML for the GridView looks like this:

<GridView
    android:id="@+id/testView"
    android:background="#eee"
    android:numColumns="5"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginStart="0dp"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="2dp"
    app:layout_constraintTop_toBottomOf="@+id/button3"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp" />

How do I solve that in a reasonable way?

EDIT: I don't want to change the size of all columns like in the possible duplicate. I just want to change the size of the first one.

Upvotes: 1

Views: 339

Answers (1)

Nouman Ch
Nouman Ch

Reputation: 4121

make use of Span Size to get the required size of your column.

GridLayoutManager manager = new GridLayoutManager(this, 10); // 3 cols of 3 + 1 col of 1
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
  @Override
  public int getSpanSize(int position) {
    if (position % 4 == 3)
       return 1; // Fourth column is 1x
    else
       return 3; // Remaining columns are 3x
  }
});
recyclerView.setLayoutManager(manager);

this is just an example of using span but not the solution of your scenario.

hope this will help you.

Upvotes: 2

Related Questions