Mob
Mob

Reputation: 45

How I can adding ListView Header to my app?

Could you let me know how I can add a header to my app?

Could anyone guide me on how to do that the easy way?

This is code for this Website:
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/

<?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" >
 
<!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search products.."
    android:inputType="textVisiblePassword"/>

<!-- List View -->
<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
<?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" >
 
<!-- Single ListItem -->
 
<!-- Product Name -->
<TextView android:id="@+id/product_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>    

</LinearLayout>
public class MainActivity extends Activity {
    
    // List view
    private ListView lv;
     
    // Listview Adapter
    ArrayAdapter<String> adapter;
     
    // Search EditText
    EditText inputSearch;
     
     
    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                "iPhone 4S", "Samsung Galaxy Note 800",
                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
         
        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
         
        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);
         
        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {
             
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);   
            }
             
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });
    }    
}

Upvotes: 2

Views: 162

Answers (3)

Rawa
Rawa

Reputation: 13916

on the ListView use method addHeaderView(View v)

View header = getLayoutInflater().inflate(R.layout.header, null);
lV.addHeaderView(header)

Where R.layout.header is a resource containing the layout of your header.

Please in the future google or the search function on stackoverflow, simple questions like these are already answered or very easy to find on the web. For learning how to use the ListView i suggest you take this tutorial.

Upvotes: 1

Spring Breaker
Spring Breaker

Reputation: 8251

Do something like below,

LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv , false); //header is your layout in which you can add what ever view you want.
lv.addHeaderView(header, null, false);

But add it before setting the adapter of the ListViewotherwise header won't show up.

Reference http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View,%20java.lang.Object,%20boolean)

Upvotes: 1

Syed Nazar Muhammad
Syed Nazar Muhammad

Reputation: 625

This can be acheive by making a separate layout for your header, after that inflate that layout and perform addHeaderView(inflated view) to your list.. after doing this set listview adapter.

Upvotes: 1

Related Questions