Currently I have a class that is extending the ListActivity class. I need to be able to add a few static buttons above the list that are always visible. I've attempted to grab the ListView using getListView() from within the class. Then I used addHeaderView(View) to add a small 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 I set the adapter I do:
ListView lv = getListView();
lv.addHeaderView(findViewById(R.layout.header));
This results in nothing happening to the ListView except for it being populated from my database. No buttons appear above it.
Another approach I tried as adding padding to the top of the ListView. When I did this it successfully moved down, however, if I added any above it, it pushed the ListView over. No matter what I do it seems as though I cannot put a few buttons above the ListView when I used the ListActivity.
Thanks in advance.
synic, I tried your suggestion previously. I tried it again just for the sake of sanity, and the button did not display. Below is the layout file for the activity and the code I've implemented in the oncreate().
//My listactivity I am trying to add the header 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>
findViewById()
only works to find subviews of the object View. It will not work on a layout id.
You'll have to use layout inflater to convert the xml to it's corresponding View components. Something like this:
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 wasn't just throwing an error. findViewById()
was probably just returning null
, and so no header was added to your list.
Here is the simpliest sollution:
<?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 button you can add another horizontal linear layout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With