Rajeev Kumar Rajeev Kumar - 9 days ago 6
Java Question

Set ListView Height Programmatically

I have a

TextView
and
ListView
in my activity.
ListView
intially set to
Visibility = gone
I need a functionality like if user click on
TextView
,
ListView
binds and will show to `Wrap_Content'. I have write the code so far

My XMl File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtDate"
android:textSize="50dp"
android:background="@color/colorSplash"
android:text="20-11-2016"/>

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lstDesc"
android:visibility="gone" />




Java Code

private void BindBookedList(){
final ArrayList<String> arr = new ArrayList<>();
arr.add("21-11-2016");
arr.add("22-11-2016");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_collapse, arr){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_collapse, null);
}
final TextView txtDate = (TextView) v.findViewById(R.id.txtDate);
final ListView lstDesc = (ListView) v.findViewById(R.id.lstDesc);
String strItemDAta = arr.get(position);
txtDate.setText(strItemDAta);
String[] arrContries = getResources().getStringArray(R.array.ListOfAirport);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getApplicationContext(), R.layout.spinner_item, arrContries);
lstDesc.setAdapter(adapter);
txtDate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {

com.kommlabs.pikmybox.MyUtlilites.ViewAnimationUtils.expand(lstDesc);
}
});
return v;
}
};
ListView listBooked = ((ListView) findViewById(R.id.lstBookedPackages));
listBooked.setAdapter(adapter);
}


ViewAnimationUtils class

public class ViewAnimationUtils {
static View view;
public static void expand(View v) {
view = v;
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
v.setVisibility(View.VISIBLE);

}


In this code, i am calling
Expand
function of
ViewAnimationUtils
class. In expand function i have set layout height to
Wrap_Content
but Listview render height upto show only one item.
Listview
has 8 items on bind.

How to set height of
ListView
to show all items in list ?

Answer

This will help you.

 ListAdapter listadp = listview.getAdapter();
       if (listadp != null) {
           int totalHeight = 0;
           for (int i = 0; i < listadp.getCount(); i++) {
               View listItem = listadp.getView(i, null, listview);
               listItem.measure(0, 0);
               totalHeight += listItem.getMeasuredHeight();
           }
           ViewGroup.LayoutParams params = listview.getLayoutParams();
           params.height = totalHeight + (listview.getDividerHeight() * (listadp.getCount() - 1));
           listview.setLayoutParams(params);
           listview.requestLayout();