Reputation: 837
How to add button in footer below listview in layout with fragment. I have code as below:
In fragment java.
ListView listview;
Button button;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category, container, false);
View footerView = inflater.inflate(R.layout.footer_category, null);
listview = (ListView) view.findViewById(R.id.categoryNewsItemGrid);
listview.addFooterView(footerView);
loadmore = (Button) footerView.findViewById(R.id.loadMore);
loadmore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("Mylog","Button can click");
}
});
return view;
}
/res/layout/category.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"
android:orientation="vertical" >
<ListView
android:id="@+id/categoryNewsItemGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#C5C5C5"
android:dividerHeight="2px" />
</LinearLayout>
/res/layout/footer_category.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footerCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/loadMore"
android:layout_width="fill_parent"
android:text="Load More" />
</LinearLayout>
And i get error(Force Close), if i see in logcat, i get message error Android Runtime in line 74, line 74 is:
View footerView = inflater.inflate(R.layout.footer_category, null);
My question, i want to add button in footer, below listview. like as this screenshot. thanks.
Upvotes: 1
Views: 6927
Reputation: 30874
You missed layout_height
in your /res/layout/footer_category.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footerCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/loadMore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load More" />
</LinearLayout>
Upvotes: 3