Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 246
Rajeev Kumar Avatar asked Nov 29 '16 08:11

Rajeev Kumar


3 Answers

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();
like image 76
Rushi Ayyappa Avatar answered Oct 17 '22 19:10

Rushi Ayyappa


you can try this function to update the height of listView

public static void updateListViewHeight(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        return;
    }
    // get listview height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount();
    for (int size = 0; size < adapterCount; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    // Change Height of ListView
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = (totalHeight
            + (myListView.getDividerHeight() * (adapterCount));
    myListView.setLayoutParams(params);
}
like image 4
steevoo Avatar answered Oct 17 '22 20:10

steevoo


If you want to add ListView on ScrollView and adjust the height of ListView try this code

if (adapter != null) {
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
         View listItem = adapter.getView(i, null, listView);
         listItem.measure(0, 0);
         totalHeight += listItem.getMeasuredHeight() + (listItem.getMeasuredHeightAndState()/2);
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
like image 1
Owais Bin Rizwan Avatar answered Oct 17 '22 21:10

Owais Bin Rizwan