1

enter image description here

I've created a RecyclerView adapter that sizes the viewholder based on 33% of the screen size.

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.viewholder_home_action, parent, false);
        ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
        int width = (int) (parent.getWidth() * 0.33);
        layoutParams.width = width;
        layoutParams.height = width;
        itemView.setLayoutParams(layoutParams);
        return new ViewHolder(itemView);
    }

The problem is that there is alot of space on the bottom of the ViewHolders. My question is how do I set the Gravity of the ViewHolders to Center Vertically?

Steps I've tried:

Adding gravity to the RecyclerView

This is the layout for the Fragment containing the Recycler:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/darkThemeBgrColor">

    <ImageView
        android:id="@+id/iv_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.70"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_gravity="center_vertical" <------- Does not work 
        android:layout_height="0dp"
        android:paddingStart="3dp"
        android:paddingEnd="3dp"
        android:background="@color/white"
        android:clipToPadding="false"
        app:layout_constraintTop_toBottomOf="@+id/guideline_horizontal"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Adding Gravity to the ViewHolder:

My ViewHolder uses a ViewGroup of ConstraintLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="3dp"
    android:layout_marginEnd="3dp"
    android:layout_gravity="center_vertical"> <------ Does not work

enter image description here

This only displays the gravity working in the xml preview, but unfortunately does nothing to the viewholder.

Another thing I've tried:

Casting the LayoutParams to a Constraint Layout

ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) itemView.getLayoutParams();

but it doesn't have any methods to set the Gravity:

enter image description here

Fourth thing that I've tried:

Setting the Gravity of the ItemView:

@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.viewholder_home_action, parent, false);
        ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
        int width = (int) (parent.getWidth() * 0.33);
        layoutParams.width = width;
        layoutParams.height = width;
        itemView.setLayoutParams(layoutParams);
        //Added one extra line of code here to set the item view gravity:
 
itemView.setForegroundGravity(Gravity.CENTER_VERTICAL);
        return new ViewHolder(itemView);
    }

Also unfortunately does nothing.


A bit lost on what to do. Does anyone have an idea of how would I add gravity to my viewholders programmatically so that they're centered vertically?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • you need to dynamically get the screen size and then set margins to your view items programmatically – Shayan Samad Jun 14 '22 at 10:02
  • I don't think I want to add margins programmatically which will make it too complex. I think the problem can be solved with a gravity implementation, but I don't know how to do so. – DIRTY DAVE Jun 14 '22 at 10:06

2 Answers2

2

You need to add constrain to childView and make ViewHolder match_parent

Adapter

class TestAdapter: RecyclerView.Adapter<TestViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
        val viewBinding = ItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val view = viewBinding.root
        val layoutParams: ViewGroup.LayoutParams = view.layoutParams
        val width = (parent.width * 0.33).toInt()
        layoutParams.width = width
        view.layoutParams = layoutParams

        return TestViewHolder(viewBinding, parent.context)
    }

    override fun onBindViewHolder(holder: TestViewHolder, position: Int) {

        holder.setData()
    }

    override fun getItemCount(): Int {
        return 10
    }
}

ViewHolder

class TestViewHolder(private val viewBinding: ItemBinding,
                     private val context: Context) : RecyclerView.ViewHolder(viewBinding.root) {
    fun setData() {
        val width = (context.resources.displayMetrics.widthPixels * 0.33).toInt()
        val layoutParams = viewBinding.cardView.layoutParams
        layoutParams.width = width
        layoutParams.height = width
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:padding="4dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/ic_launcher" />
    </androidx.cardview.widget.CardView>

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

enter image description here

Hope this helps

GHH
  • 1,713
  • 1
  • 8
  • 21
  • ```You need to add constrain to childView``` I am a bit confused on which childView you are talking about. Could you please clarify? – DIRTY DAVE Jun 15 '22 at 01:49
  • @DIRTYDAVE Sorry for my wrong wording, i add some code.Check my update – GHH Jun 15 '22 at 02:06
  • I just tried it, ```match_parent``` to the parent layout and also ```bottom_toBottomOf``` constraint but it's not working for me. https://i.imgur.com/VtjP6nT.png Can you also post your adapter code? – DIRTY DAVE Jun 15 '22 at 02:15
  • I have also even try to add the constraint programmatically. https://i.imgur.com/UewhipT.png but still doesn't work – DIRTY DAVE Jun 15 '22 at 02:17
  • 1
    @DIRTYDAVE Check my update. I just use kotlin code and you can trans into java code – GHH Jun 15 '22 at 02:20
  • PROBLEM SOLVED!! After comparing our adapter code, I noticed I am setting the ```layoutParams.height```, but you are not. So after I remove this line: ```layoutParams.height = width;``` it finally centers it. Thank you for your help! – DIRTY DAVE Jun 15 '22 at 02:24
0

you can't set gravity for RecyclerView, because it isn't needed, not in this case. your RecyclerView shoud have height set to wrap_content and no app:layout_constraintTop_toBottomOf="@+id/guideline_horizontal" attribute. keep app:layout_constraintBottom_toBottomOf="parent" for aligning to parents bottom as you wish and wrap_content will make RecyclerView draw its childerns without any empty space on bottom

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="3dp"
    android:paddingEnd="3dp"
    android:background="@color/white"
    android:clipToPadding="false"
    app:layout_constraintBottom_toBottomOf="parent"/>
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I've tried using ```wrap_content``` already but it makes the entire recyclerview items and recycler not visible (probably something to do with the sizing in onCreateViewHolder). That's why I was using 0dp and the guideline. It also works with a specified size e.g 300dp. – DIRTY DAVE Jun 14 '22 at 10:10