Android: adding a static title to the top of ListActivity

I currently have a class extending the ListActivity class. I need to add some static buttons above the list that are always visible. I tried to grab a ListView using getListView () from the class. Then I used addHeaderView (View) to add the layout to the top of the screen.

Header.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/testButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Income" android:textSize="15dip" android:layout_weight="1" /> </LinearLayout> 

Before installing the adapter, do the following:

 ListView lv = getListView(); lv.addHeaderView(findViewById(R.layout.header)); 

This leads to the fact that nothing happens with the ListView, except that it populates from my database. Buttons do not appear above it.

Another approach that I tried to add to the top of the ListView. However, when I did this, it successfully moved down, however, if I added something above, it moved the ListView. No matter what I do, it seems like I can't put a few buttons above the ListView when I used ListActivity.

Thanks in advance.

synic, I have tried your suggestion before. I tried it again for the sake of common sense, and the button did not appear. Below is the layout file for the activity and the code that I implemented in oncreate ().

// My listactivity I'm trying to add a title to

 public class AuditActivity extends ListActivity { Budget budget; @Override public void onCreate(Bundle savedInstanceState) { Cursor test; super.onCreate(savedInstanceState); setContentView(R.layout.audit); ListView lv = getListView(); LayoutInflater infalter = getLayoutInflater(); ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false); lv.addHeaderView(header); budget = new Budget(this); /* try { test = budget.getTransactions(); showEvents(test); } finally { } */ // switchTabSpecial(); } 

Layout.xml for activity:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty" /> </LinearLayout> 
+46
android header listactivity
Apr 12 '10 at 6:13
source share
5 answers

findViewById() only works for finding subzones of a View object. It will not work with the layout id.

You will need to use a layout inflatable element to convert xml to the corresponding View components. Something like that:

 ListView lv = getListView(); LayoutInflater inflater = getLayoutInflater(); View header = inflater.inflate(R.layout.header, lv, false); lv.addHeaderView(header, null, false); 

I'm not sure why your code has not just thrown an error. findViewById() probably just returned null , so no title was added to your list.

+91
Apr 12 2018-10-12T00:
source share

Here is the simplest solution:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background"> <include layout="@layout/actionbar"/> <ListView android:id="@+id/tasklist_TaskListView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:textColor="@color/baseFont"/> <include layout="@layout/bottombar"/> </LinearLayout> 

or

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ListView android:id="@+id/tasklist_TaskListView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:textColor="@color/baseFont"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> 

instead of a button, you can add another horizontal line layout

+8
Nov 04 2018-10-18T00:
source share

After some research, I was able to find out that by mixing TableLayout and LinearLayout in my ListActivity XML document, I was able to add a title to the document. Below is my XML document if anyone is interested in seeing it. Although the syntactic approach is probably the right approach after working with its solution for a while, I could not get it to function the way I wanted it to.

AuditTab.java

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audittab); getListView().setEmptyView(findViewById(R.id.empty)); } 

audittab.xml

 <?xml version="1.0" encoding="utf-8"?> <TableLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent"> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/btnFromDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_weight="1" /> <Button android:id="@+id/btnToDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_toRightOf="@+id/btnFromDate" android:layout_weight="1" /> <Button android:id="@+id/btnQuery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Query" android:layout_toRightOf="@+id/btnToDate" android:layout_weight="1" /> </LinearLayout> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@android:id/list" android:layout_width="300dip" android:layout_height="330dip" android:scrollbars="none" /> <TextView android:id="@+id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dip" android:text="- Please select a date range and press query." /> </LinearLayout> </TableRow> </TableLayout> 

AuditItem.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="horizontal" android:padding="10sp"> <TextView android:id="@+id/transactionDateLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date: " /> <TextView android:id="@+id/transactionDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/transactionDateLabel" /> <TextView android:id="@+id/transactionTypeLabel" android:layout_below="@id/transactionDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Type: " /> <TextView android:id="@+id/transactionType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionTypeLabel" /> <TextView android:id="@+id/transactionAmountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:text="Amount: " android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionType" /> <TextView android:id="@+id/transactionAmount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionAmountLabel" /> <TextView android:id="@+id/transactionCategoryLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Category: " android:layout_below="@id/transactionAmountLabel" /> <TextView android:id="@+id/transactionCategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionAmountLabel" android:layout_toRightOf="@id/transactionCategoryLabel" /> <TextView android:id="@+id/transactionToAccountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To Account: " android:layout_below="@id/transactionCategoryLabel" /> <TextView android:id="@+id/transactionToAccount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/transactionCategoryLabel" android:layout_toRightOf="@id/transactionToAccountLabel" /> <TextView android:id="@+id/transactionFromAccountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="From Account: " android:layout_below="@id/transactionToAccountLabel" /> <TextView android:id="@+id/transactionFromAccount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionToAccountLabel" android:layout_toRightOf="@id/transactionFromAccountLabel" /> <TextView android:id="@+id/transactionNoteLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Note: " android:layout_below="@id/transactionFromAccountLabel" /> <TextView android:id="@+id/transactionNote" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionFromAccountLabel" android:layout_toRightOf="@id/transactionNoteLabel" /> <Button android:id="@+id/editTransactionBtn" android:layout_width="wrap_content" android:layout_height="40sp" android:visibility="gone" android:text="Edit" android:layout_below="@id/transactionNoteLabel"/> <Button android:id="@+id/deleteTransactionBtn" android:layout_width="wrap_content" android:layout_height="40sp" android:text="Delete" android:layout_below="@+id/transactionNoteLabel" android:visibility="gone" android:layout_toRightOf="@+id/editTransactionBtn" android:ellipsize="end"/> </RelativeLayout> 
+7
Apr 30 '10 at 4:23
source share

ListView's answer above is useful, but it scrolls the list and doesn't save the graphic title at the top. The best solution I've found is to set a custom title for the activity. This is what my constructor looks like:

 public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.your_listview_layout); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.your_header); ... 

Where your_listview_layout.xml customizes the ListView and your_header.xml contains any custom header layout you like. Just note that the three lines above should be called in that order so as not to cause problems at runtime.

The tutorial that helped me was http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ , and you can find many related pages in "Stack Overflow" by doing search term "setFeatureInt"

+4
Oct. 15 2018-11-11T00:
source share

Adding a static header is easy, just create a separate relative view for which the alignParentTop attribute (or bottom, right or left) is set to true.

0
Oct 14 '10 at 16:21
source share



All Articles