15

My target is simple. I want to add a "Red Rectangle" as a headerview to a ListView. so I create a simple activity

public class MainActivity extends Activity {
    private String[] adapterData = new String[] { "Afghanistan", "Albania", "Algeria", 
            "American Samoa", "Andorra", "Angola"}; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv;
        lv = (ListView)findViewById(R.id.list);

        LayoutInflater lf;
        View headerView;
        lf = this.getLayoutInflater();
        headerView = (View)lf.inflate(R.layout.header, null, false);

        lv.addHeaderView(headerView, null, false);

        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, adapterData)); 
    }
}

activity_main.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0" 
        android:cacheColorHint="#0000"
        android:paddingLeft="20dip"
        android:paddingRight="20dip"
        android:divider="#0000"
        android:scrollbarStyle="outsideOverlay" />

list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:minHeight="40dip"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
/>

most important, header.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dip"
    android:background="#F00"/>

But it doesn't seem to work. Nothing is displayed.

Can any give some suggestions on this? Your help is highly appreciated.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
user1764413
  • 151
  • 1
  • 1
  • 3

4 Answers4

16

Old question, but I just copied it and run it and indeed the red doesn't show up.

You're missing one simple thing: Regardless of how you call inflate you always1 have to pass a parent view (second param) so that it can figure out which LayoutParams subclass to instantiate.
1: there are always1 exceptions

.inflate(R.layout.header, lv, false)

For more on the subject I highly suggest: http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ (worth reading even a few times)

Your full code modified (xml files can stay as is):

public class MainActivity extends Activity {
    private static final String[] ADAPTER_DATA = {
            "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list = (ListView) findViewById(R.id.list);
        View header = getLayoutInflater().inflate(R.layout.header, list, false);

        list.addHeaderView(header, null, false);
        list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, ADAPTER_DATA));
    }
}
TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • Thank you this solved my issue! The problem in my case was just the missign "false" parameter in View header = getLayoutInflater().inflate(R.layout.header, list, false); ! – Recomer Apr 18 '16 at 19:36
  • The inflated header view may be bare-bone, how can I add values to the sub-views in the layout (such as texts, etc.) at runtime? – TechAurelian Apr 24 '20 at 05:19
13

This is how I add a header:

    View view = View.inflate(context, R.layout.header, null);
    lv = (ListView) findViewById(R.id.list);


    lv.addHeaderView(view);
Laura
  • 2,653
  • 7
  • 37
  • 59
2

Check this, I have get it working right now:

    ListView lv = (ListView)findViewById(R.id.list);

    LayoutInflater layoutInflater = this.getLayoutInflater();
    View headerView = (View) layoutInflater.inflate(R.layout.header, lv, false);

Just send listview to the second param of the inflate method :)

BamsBamx
  • 4,139
  • 4
  • 38
  • 63
1

You should declare the header view in a separated xml file. Then, inflate it in to a View instance and set it to ListView.

View view = ... (inflate the view from xml file)
listview.addHeaderView(view)

You must call addHeaderView before setAdapter to the listview.

Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • yes, just as the code I post, I declare header.xml as a seperated xml file. and I call addHeaderView before setAdapter to the listview. – user1764413 Oct 22 '12 at 06:17
  • Let take a look at http://www.samcoles.co.uk/mobile/android-listactivity-with-a-header-or-footer/. Hope this helps. – Nguyen Minh Binh Oct 22 '12 at 06:29