I am working on android. I want my list view to wrap its content horizontally and not to fill all the width. The wrap_content properties is not working. What to do?
5 Answers
In order achieve wrap_content
height in ListView
, we need to use CustomListView
that extends
our native ListView
.
MyListView.java
public class MyListView extends ListView {
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
in your layout
use custom view like this,
layout.xml
<com.yourpackagename.MyListView
...
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />

- 16,558
- 7
- 41
- 45
-
1Fantastic! Worked for me when I wanted to use a ListView inside a RecyclerView. – Vahid Amiri Jan 23 '17 at 20:19
-
4This answer is useful, but when ListView is inside a RecyclerView, scrolling up appears to be buggy. So the real solution is to override onMeasure method. See this answer: http://stackoverflow.com/a/24186596/1901561 – RIscRIpt Apr 16 '17 at 21:00
-
1Doesn't work if, for example, the second row is bigger than the first. – Alec von Barnekow Jun 02 '18 at 09:25
-
2Height is working good, what about width, width taking full length, If I taking wrapcontnet as width not working – rams Sep 17 '19 at 11:09
-
Worked like a charm – Saad Zahoor May 26 '21 at 03:08
-
Doesn't work for me. In my case, the parent layout is RelativeLayout and layout_height="wrap_content". – Talha Ç May 16 '22 at 14:22
As Romain Guy (Google Engineer works on UI toolkit) Said in his post
By setting the width to wrap_content
you are telling ListView to be as wide as the widest of its children. ListView must therefore measure its items and to get the items it has to call getView() on the Adapter. This may happen several times depending on the number of layout passes, the behavior of the parent layout, etc.
So if you set the layout width or layout height of your ListView to wrap_content
the ListView will try to measure every single view that is attached to it - which is definitely not what you want.
Keep in mind: avoid setting wrap_content
for ListViews or GridViews at all times, for more details see this Google I/O video talking about the world of listview
-
9This advice doesn't exactly seem universal. The ListView/Adapater combination is more generic and scalable than people might realize at first, which means that people also might not realize the consequences of using wrap_content with arbitrary datasets. But that's not to say that there aren't plenty of cases where the dataset is of a fixed or limited size. In those cases, using wrap_content would be more of a judgement call. – spaaarky21 Jun 19 '13 at 22:43
-
3This seems like something lint should warn about - but it currently doesn't. – Someone Somewhere Sep 13 '13 at 20:21
-
2I don't know why this answer was accepted: it doesn't answer the question at all. It also parrots back the Romain's Guy post - which was about a completely different question: the number of times a layout is measured - and points to a video that actually explain why wrap_content should work, at least for 3 list items, and "sort of work" for more complex lists (at a considerable performance cost). – Rick77 Aug 19 '17 at 20:35
-
until now, the lint does not do this. could have saved me from hours of trying! – user1506104 Dec 18 '17 at 11:46
It can be implemented by using LinearLayout.
Create like this example:
public class LinearLayoutAdapter {
LinearLayout linearLayout;
ArrayList<String> arrayList
private View rootView;
private static LayoutInflater inflater;
public ChoicesAdapter_(ArrayList<String> arrayList ,LinearLayout linearLayout)
{
this.index =index;
this.arrayList=arrayList;
this.linearLayout=linearLayout;
notifyDataSetChanged();
}
private void notifyDataSetChanged() {
linearLayout.removeAllViews();
for (int i =0;i<getCount();i++){
linearLayout.addView(getView(i,null,null));
}
}
public int getCount() {
return arrayList.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
rootView = inflater.inflate(R.layout.row_list, null);
TextView textView = (TextView)rootView.findViewById(R.id.text);
textView.setText(arrayList.get(position));
return rootView;
}
}
MainActivity example:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Accent");
arrayList.add("Acclaim");
arrayList.add("Accord");
arrayList.add("Achieva");
arrayList.add("Aerio");
arrayList.add("Aerostar");
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
linearLayoutAdapter= LinearLayoutAdapter(arrayList,linearLayout);
}
}
XML example:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="@color/dark_gray"
android:background="@color/white"
android:textSize="20dp"
android:text="" />

- 6,554
- 4
- 71
- 89

- 155
- 1
- 6
-
Yes, you can do this, but you loose (or have a lot to implement) the recycle system for listitem views. It works fine for few items but, with few items you may consider not using a listview neither an adapter. – Ratata Tata May 26 '17 at 22:34
I want my list view to wrap its content horizontally and not to fill all the width.
In case of List View you can't give wrap_content , because if you give wrap_content for List View only first three items(rows) will be shown rest will be ignored.. So don't use wrap_content.. Always use fill_parent or match_parent for List View
For efficient design on List View you can see this World of ListView

- 11,049
- 5
- 49
- 66
-
2Don't use fill_parent. It was deprecated in Android 2.2: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT – spaaarky21 Jun 19 '13 at 22:47
I know I am little too late for answering this question. But in my opinion, if you want your ListView to have somewhat of a wrap_content behavior, you can set its start and end margins to be some static value. This worked in my case. If you are concerned about doing it for various screen sizes, there is a really helpful library to handle this scenario. Scalable DP. Do check it once in github.

- 494
- 8
- 24