I am displaying a listView on my ListViewActivity. And I would like to add a button to the top of the LisView. I have tried this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
setContentView(R.layout.listview_singlegrey);
LinearLayout linear = (LinearLayout) findViewById(R.id.list_comment);
Button btAddComment = new Button(this);
btAddComment.setText("Añadir comentario");
linear.addView(btAddComment);
setContentView(linear);
//....
}
The file listview_singlegrey.xml is as follows:
<?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" android:id="@+id/list_comment"
android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/fondomain">
<ListView android:id="@+id/android:list" android:listSelector="@android:color/transparent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:divider="@layout/linedivider" android:dividerHeight="10px"
android:cacheColorHint="#0000" android:paddingTop="10dp"
/>
</LinearLayout>
But I cant see the button. Any ideas why?
If you want to add button to the top of your listview, you must add that to zero’th index
Now your listview is in index 1.