In iOS programming I have to implement (or at least if I don't want the default one...) an method which returns the height of the row. I am probably blind because I can't find the method where I set the height of the row in a listview in Android.
I have created custom row layouts, so I start to think that I set the height in the parent layout manager in the XML. Can anyone confirm that this is the way to do it? Or is it a method?
You'll need to set the height in the layout xml for the row itself. If you absolutely want every row to be the same height, set:
android:minHeight="80dp"
android:maxHeight="80dp"
in your base layout (Should be a LinearLayout or RelativeLayout, likely) of the listview row. Note you can do:
android:minHeight="?android:attr/listPreferredItemHeight"
android:maxHeight="?android:attr/listPreferredItemHeight"
to be inline with (most) other applications.
Yes, the only way to set the height of the row is to mention it's dimensions in the xml ,for the list item or you can also set the height of the list item in the getView() method of listView if you have a custom Adapter. There is no listView method as such to define the height of the row.
Related
I am trying to make the rows of my listview such that clicking on any one expands that row in place and shows extra options. I use an extra layout (let's call it expandView) contained with the row layout (call it rowView) with height set to zero initially and use a valueanimator to expand it to the final height when rowView is clicked.
Problem is, I have no idea what the final height is going to take until the user actually clicks the rowView. So I cannot specify some fixed end value for the animation, nor can I sum up the heights of expandView's children on rowView click since they too return zero when expandView's height is zero.
I really want to avoid hardcoding the height values for expandView or it's children here. Any pointers?
Figured it out. This answer given here helped. I have to call call measure() followed by getMeasuredHeight() to get the final height of my expandView.
I add a gridview as headerview of listview,when i change gridview's content, it's height does't changes,I can only see a part of gridview's new content.I have tried invalidate,but it does't work.In the gridview,i click the more button,and then change the adapter of gridview,but i can see only a part of new content of gridview,and when i call geidview.getHeight,(),it does't change
gridView.getHeight() will not return the height of the gridView. It would return 0. This is because the gridView would get layed out only after the view has been rendered. You could check the android documentation for this.
If you want to add the header, then set a minHeight on the gridView something like :
<GridView
android:minHeight="5dp" />
This will ensure that the header has a minHeight of 5dp.
I'm developing an android app with fragments. While most of my layouts are pre-determined in the XML, I would like to programmatically insert a new view between views that were already loaded in a LinearLayout at startup.
How do I go about with this?
Thanks
Its possible to specify index while u dynamically add a view to a LinearLayout.
Set height of the first view as
android:layout_height="0dp"
android:layout_weight="1"
Set height = wrap_content for the second view in XML
Then while u are adding new View dynamically, set its height = wrap_content and add it to the parent LinearLayout like this
parentLinearLayout.addView(childView, index);
//index = position where you want to insert the new view.
It might help you. :)
the red View should have the default setting View.setVisibility(View.GONE) right at the beginning. When its time to show up you can switch over to View.setVisibility(View.VISIBLE). I cant verify the solution right now, but it should do the trick. So in this case you are not inserting a new View but make an existing one visible.
I have a listview for display message, and I wanna add footerview to show loadmore button, if the listview set android:layout_height="fill_parent" it works normally, but I need to give it an absolute value to set listview height such as android:layout_height="400dip" so as to avoid getview method repeat calling , and then the footerview width can’t match parent.
Set the list view layout_height="fill_parent" and layout_weight="1", then anything that you put under it will have space.
Is there a way to declare a row of buttons in XML so that all the buttons have the same width, which is equal to the wrap_content width of the widest button? I'm familiar with the trick of setting all the widths to 0 and assign them all a weight of 1, but that won't work if the parent layout width is set to wrap_content. I don't want to set the parent width to fill_parent because I don't want the buttons stretched more than necessary.
The only way I can think of doing this is in code (either with onMeasure logic in each button that communicates with the other buttons or with a custom layout class).
I think you'd have to do this in code.
Creating a custom layout class would be the way to go. Override onMeasure() and make it look something like this:
Call setLayoutParams on all children to set their layout_widths to WRAP_CONTENT.
Call super.onMeasure()
Iterate child views to find the one with the biggest getMeasuredWidth().
Iterate all other child views calling setLayoutParams() with the widest pixel width.
Call super.onMeasure() again. :)
That should work but I won't stake my reputation on it... happy to help you further if it doesn't.
To get the buttons in a row in the XML you need to add the buttons with in a LinearLayout and change the orietnation to horizontal i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>
As for getting the widest button and changing all the other buttons to match I am not sure as a guess you would have to have some sort of method within your activity to get the widest button and then programaticaly set all the other buttons to be the same.
Just find out what your widest button is, but it in a view with a horizontal width to match, and then use layout_width="match_parent" or "fill_parent" in < 2.3.
It'll make them all use the width assigned.
If you want to do it programatically, you need to iterate over all the sections, find the max, than iterate again and set it.