Set Header in List View

            Hear I am going to explain how you can set header in List view with customize layout or action ,like hear I am explain you how you can add new item using header button In list view.
            For add header in list view “addheaderview()” method used and for footer “addfooter()” is used.


Layout File : activity_main.xml //main layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.scanwifi.MainActivity">
    <ListView
        android:id="@+id/listof_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
Layout File : demo_header.xml //for row of wifi list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:padding="10dp"
    android:background="#fff"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add new Item"
        android:textSize="15dp" />
</RelativeLayout>

Class File : MainActivity.java // main java class file


package com.dhaval.demo;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
   ListView listof_item;
    ArrayList<String> numofRecord;
    Button btnClick;
    ArrayAdapter<String> dataAdapter;
    ArrayList<String> data;
    int c = 0;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listof_item = (ListView) findViewById(R.id.listof_item);
        data = new ArrayList<>();
        data.add("data1");
        data.add("data2");
        data.add("data3");
        data.add("data4");
        data.add("data5");
        data.add("data6");
        c = 6;
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.demo_header, listof_item, false);
        btnClick = (Button) viewGroup.findViewById(R.id.btnClick);
        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                c = c + 1;
                data.add("New Data Added > " + c + "< Added");
                dataAdapter.notifyDataSetChanged();
                Toast.makeText(getBaseContext(), "You clicked on Header Button", Toast.LENGTH_SHORT).show();
            }
        });
        listof_item.addHeaderView(viewGroup);
        dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        listof_item.setAdapter(dataAdapter);
    }
}

No comments: