Android: Adding Static Header to the Top of a Listactivity

Android: Adding static header to the top of a ListActivity

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.

How to fix the place of header in ListView?

The better way is set the header statically in a separated layout (ouside your ListView). An example that illustrates this could be:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<View
android:id="@+id/YOUR_HEADER_VIEW"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

You have to replace the View with your header's view.

I hope this solve your problem.

How to add static header image in custom listview?

Remove the image form the xml.

ImageView iv = new ImageView(getActivity());
// set image to imageview
ListView list = getListView();
list.addHeaderView(headerView);

The docs

public void addHeaderView (View v)

Added in API level 1
Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.

Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.

Parameters
v The view to add.

Edit:

@Override
public void onActivityCreated(Bundle savedInstanceStat)
{
super.onActivityCreated(savedInstanceState);
list = getListView();
ImageView imageHeaderView = new ImageView(getActivity());
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pantaigoacina));

list.addHeaderView(imageHeaderView);

List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

for (int i = 0; i < DaerahWisata.pantai.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("pantai", DaerahWisata.pantai[i]);
hm.put("detail", "Lokasi : \r\n" + DaerahWisata.detailp[i]);
aList.add(hm);
}

String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };

SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), aList, R.layout.fragment_detail, from,
to);
setListAdapter(adapter);
}

Edit, if want to set imageview from other xml layout :

list = getListView();
Context context = getListView().getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image_header, null);
list.addHeaderView(view);

Android ListView with fixed header and footer

I solved it by using @blackbelt suggestion and a small ImageView with the source image being transparant with a tile background.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@+id/tv_footer"
android:layout_below="@+id/tv_header" />

<TextView
android:id="@+id/tv_footer"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/footer_bg"
android:gravity="center"
android:text="Footer" />

<TextView
android:id="@+id/tv_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/header_bg"
android:gravity="center"
android:orientation="vertical"
android:text="Header" />

<ImageView
android:id="@+id/iconView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/lv"
android:background="@drawable/header_bg2"
android:src="@drawable/transparant_bg_tile" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tv_footer"
android:layout_alignParentRight="true"
android:background="@drawable/footer_bg2"
android:src="@drawable/transparant_bg_tile" />

</RelativeLayout>

Screenshot from device
Sample Image

Android ListActivity - fixed header and footer

You can achieve it by using a custom XML layout in which you will set the layout of your header, footer and list.

Note that to be compatible with ListActivity this layout must contain a ListView with the id android.R.id.list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HEADER" />

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FOOTER" />

</LinearLayout>

And set it in your ListActivity like this:

public class TestActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Static header in ListView, CustomBaseAdapter

Afaik, all that addHeaderView() does is add the item at the top of the list, but it is not supposed to make it "fixed" (unscrollable). If you want to have an item that's fixed, it sounds like you should have two views arranged in a vertical LinearLayout: the top one is the fixed part, and the bottom one is a ListView.



Related Topics



Leave a reply



Submit