Home » Android » Controlling ListView preview items number in Android Studio

Controlling ListView preview items number in Android Studio

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a ListView which would usually only display 1-3 items at runtime, but Android Studio fills it with 24, making it overtake the whole layout. If I want to see the rest of the layout I have to either comment it out or set its visibility to gone. Is there a way I can make Android Studio only render a couple of items so my other views in the layout will be visible in the designer?

How to&Answers:

I was facing the same issue and I solved it by adding this to the ListView definition:

tools:layout_height="350dip"

If you’re using a RecyclerView, you could do it much better:

tools:itemCount="3"

Both arguments are only applied in the Android Studio preview, not in the app itself, see https://developer.android.com/studio/write/tool-attributes.

Answer:

I’m assuming right now your ListView has a height that wraps content, which means it will be as long as it can to display everything. If you want to limit it, it you can give it a specific dp height or you can give it a gravity (to make it say half the screen).