16

I'm working with a ListView that is part of an ListFragment. I want to add a header with a specific/custom height but it does not matter what I put in the height of the header view, always it has the same height. Is possible to modify the header view height of a ListView?

Below the code I use:

header.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp" />

ListFragment

 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        TextView fakeHeader = (TextView)getActivity().getLayoutInflater().inflate(R.layout.header, null);
        getListView().addHeaderView(fakeHeader);
    }
Androider
  • 441
  • 2
  • 6
  • 17

1 Answers1

34

When you pass in null as the second parameter to inflate(), you are saying there is no parent associated with the view. This has the unfortunate side effect where any layout_ attribute you use is ignored as layout_ parameters affect how the parent lays out the child view rather than directly affect the child (like other views): this pro-tip godes into more details on layout_ attributes.

You can use inflate(R.layout.header, getListView(), false) to pass in the ListView which will eventually the parent of your header view (note the false says to not add the view directly - ListView will do this automatically for you).

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    Hi @ianhanniballake, your solution works like a charm :D, thank you so much! When I used _inflate(R.layout.header, getListView(), false)_ it started to work. – Androider Oct 15 '14 at 00:16
  • 2
    this is a *really* great answer, that's what you get when read the docs, folks! Shame on us – Odaym May 07 '15 at 07:14