Center items in listview vertically - android

I have a listview, the listviews width and height should be set to match parent. So, if the listview is fully stretched and has only 2 items in it, they are at the top of the listview and on the bottom is too much useless space. I want to know, if it is possible to center these 2 items in this listview vertically, if yes, how?

List height depends on the height of the list item, that is populating the list.
If you have only two items in the list why don't you simply use a Linear Layout instead with weightSum?
Anyway, if you want to achieve the result you requested in your question you have to get the display height using method
getResources().getDisplayMetrics().heightPixels in activity context.
After that you need to set the height of the list item to the height you retrieved earlier divided by 2.
And also set gravity to center_vertical for the textviews in the list.

Related

How to center Android Listview items and add dynamic dividers to match the height of the Listview

The idea is to add items to the center of the listview, and the divider between the items is of equal size and decreases if the number of items increses.
A rough sketch demonstrates what my question is about, how may i achieve this?
Are you sure you need a ListView?
Maybe you can just add the items to a vertical LinearLayout and add <Space> views (with layout_weight between them.

how to get recycleview Item height and set as container height

I've a recycle view inside a fragment (max item will show in list is 5), I want to set the fragment container size according to the number of items showing in list,
What I want is user shouldn't need to scroll the list to see all the items?
A RecyclerView is intended for showing many items in a scrolling list.
That's where it gets its name from. It efficiently Recycles the Views for items that have been scrolled off screen.
From what you are describing, you are only ever showing a maximum of 5 items and you want them all on the screen at the same time, so you don't need a RecyclerView.
Just add your items to a LinearLayout and set the layoutHeight of each item as 0dp and the layoutWeight of each item as 1. That will distribute the items evenly across the full height of the LinearLayout.
Edit
If you want the fragment to adjust it's height according to the number of items, then set the height to WRAP_CONTENT for the fragment container, LinearLayout, and each of the items you add to the linear layout.

Set the the height of all items on a row of a GridView depending on the tallest item

In an app that I'm developing I need to display a GridView with items that depending on its text (more than one line of text) may be of different height and I want to avoid that the items are shown with different heights since it's quite ugly.
When the text is only one line the GridView the items in the row are displayed properly but when the text is larger than one line they are shown like this:
How I can avoid this behavior and set the highest height of an item of a row as the height of all items of that row?

`ListView` content being added at bottom instead of top

ListView content being added at bottom instead of top
I'm using ListView to display a list of data elements. Previously the height was set to wrap_content and it was working fine but with few performance lags. Then I came across a blog saying ListView's height should not be set to wrap_content for good performance. After doing this, i.e. setting height to fill_parent, all of my data rows started appearing at bottom of the ListView instead of top. Can any body point to what possibly I'm missing. Thanks in advance.
add this attrubut to your list view android:stackFromBottom="false"

How to change the divider height of listview dynamically?

I have a listview in which there should be different divider height
between different rows. So, how can we set the divider height
dynamically?
Suppose, I have 10 rows and there should be a divider height of 5
between first 2 rows and then there should be a divider height of 1
between next 5 rows and so on.
Can someone let me know the way of doing this?
One way would be to make the dividers rows. Set them as not enabled in your isEnabled adapter method. I do that for section headers, but it is almost the same thing. Another way would be to manually lay out your whole list by implementing onLayout. If the dividers can be empty space, it might work to set top or bottom margins for the root view of your rows. In xml that would be:
android:layout_marginTop='5px'
Otherwise, just make the dividers part of the rows.

Categories

Resources