Adding title to ListView in android

I need to add a title to my list, and the title should always show even a scrollable list.

Is this possible in Android list view?

+7
source share
4 answers

The solution that works for me is to create a TableLayout that has a row with a header and a row with a list like this:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/header" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent"/> </TableLayout> 

Please make sure ListView has @android: id / list as an identifier.

+6
source

you can add the title as a list as follows:

 View headerView = ((LayoutInflater)Activity.this.getSystemService(Activity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null, false); list.addHeaderView(headerView) 
+6
source

Yes, here is what I did:

showcase

For a custom layout with ListActivity or ListFragment your layout should contain a ListView with the android:id attribute set to @android:id/list .

1) main_layout.xml :

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > // custom Headers <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center" android:text="@string/systolic" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center" android:text="@string/diastolic" /> <TextView android:id="@+id/textView3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center" android:text="@string/date" /> </LinearLayout> // This is important to be exactly like below ,android:id="@android:id/list" <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/linearLayout1" > </ListView> </RelativeLayout> 

For a custom layout for each line of the list

2) custom_list_row.xml :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/bpSysHolder" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:layout_gravity="center_vertical" android:text="TextView" > </TextView> <TextView android:id="@+id/bpDiaHolder" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:layout_gravity="center_vertical" android:text="TextView" > </TextView> <TextView android:id="@+id/bpDtHolder" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:layout_gravity="center_vertical" android:text="TextView" > </TextView> </LinearLayout> 

3) ListActivity :

 public class HistoryActivity extends ListActivity { private SimpleCursorAdapter dbAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // this is neccessery for you setContentView(R.layout.main_layout); dao = new BpDAO(this); Cursor bpList = dao.fetchAll_bp(); String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT }; int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder, R.id.bpDtHolder }; // And this for custom row layout for each list row dbAdapter = new SimpleCursorAdapter(this, R.layout.custom_list_row, bpList, from, target); setListAdapter(dbAdapter); } @Override protected void onDestroy() { super.onDestroy(); dao.close(); } @Override public void onListItemClick(ListView l, View view, int position, long id) { // TODO something on item click } } 

If you need it, check: " Attaching a sticky (fixed) header / footer to the list)

+3
source

Do one thing. Place one TextView just above the list and set the text as the title. He will reach your need.

0
source

All Articles