android – How to set height of recyclerview programmatically?-ThrowExceptions

Exception or error:

I tried,

recyclerview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,100));

But its not working.

How to solve:

Try to set height like this,

ViewGroup.LayoutParams params=recyclerview.getLayoutParams();


Came across same question just now and although ajantha’s solution works, I found a more simpler solution. If this still helps, great, glad to pay it forward.

recyclerView.getLayoutParams().height = 100;


Well, it’s too late but I have a better solution.
recyclerView.getLayoutParams().height will give you null pointer when you create a new recycler view programmatically. Here is a proper way of doing it.

RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,

Keep in mind that the first param is width while the second is height.

Leave a Reply

Your email address will not be published. Required fields are marked *