Adarsh Adarsh - 3 months ago 6
Android Question

WRAP_CONTENT not not wrapping up the listview width in android

I have a

listView
which contains a list of app icons.I am trying to set
listview
height and width programmatically like this:

listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));


But the width of the
listView
is not wrapping up according to the width of the app icons.It is just simply covering the whole screen.
I have implemented the
listView
on simple
LinearLayout
.Is there any solution.

I am using
listview
in service with
windowManager
so I set its height and width by code like this:

First of all I have set the linear layout height and width like this:

LinearLayout.LayoutParams listParam=new LinearLayout.LayoutParams(
80, ViewGroup.LayoutParams.WRAP_CONTENT
);
listLinear.setLayoutParams(listParam); //listLinear is my linearLayout


after I added listview inside the linearLayout:

listView.setId(listId);
listView.setAdapter(listadapter);
listView.setLayoutParams(new LinearLayout.LayoutParams(80, ViewGroup.LayoutParams.WRAP_CONTENT));
listLinear.addView(listView);


I want to replace 80 with WRAP_CONTENT as a width.

Answer

Try this on the ListView XML:

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#bdbdbd"
android:dividerHeight="3.0sp"
android:headerDividersEnabled="false"
android:paddingEnd="7dp"
android:paddingStart="7dp"></ListView>

I have this listview inside a FrameLayout, but you can try to have it inside a RelativeLayout.