Tuesday, 5 March 2013

Android ListView with Horizontal CheckBox.Part-2

Part-2
In previous blog i shown Listview with RadioGroup and RadioButton.
Now i am going to show how to make a list of Horizontal CheckBox.


Screenshot.1) Before selecting any Checkbox
























Screenshots.2) After Checked the list of Checkbox
























In Previous blog i used RadioGroup to achieve the ListView with horizontal RadioButton.
But for Checkbox i am going to use LinearLayout and inflate the view in Adapter class.


Steps:-

1) res/layout/activity_main.xml

RelativeLayout 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"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" />
</RelativeLayout>

2) res/layout/horizontal_checbox.xml

<?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:id="@+id/headingitem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:padding="5dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearmain"
        android:layout_gravity="center_horizontal|center_vertical"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

3) src/ListAdapter.java

package com.list.checkbox;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class ListAdapter extends ArrayAdapter<Option> {

    Context context;
    int layoutResourceId;
    Option data[] = null;

    public ListAdapter(Context context, int layoutResourceId,
            Option[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MatrixHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MatrixHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.headingitem);
            holder.position = position;
            holder.layout = (LinearLayout) row.findViewById(R.id.linearmain);
            final CheckBox[] checkbox = new CheckBox[5];
           
            for (int i = 0; i < 5; i++) {
                checkbox[i] = new CheckBox(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT);
                params.weight = 1f;
                params.setMargins(10, 0, 10, 10);
                holder.layout.addView(checkbox[i], params);
            }

            row.setTag(holder);
        } else {
            holder = (MatrixHolder) row.getTag();
        }

        Option option = data[position];
        holder.txtTitle.setText(option.title);
        return row;
    }

    static class MatrixHolder {
        TextView txtTitle;
        LinearLayout layout;
        int position;
    }
}

4) finally Your MainActivity.java

package com.list.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Option weather_data[] = new Option[]
                    {
                        new Option("Heading1"),
                        new Option("Heading2"),
                        new Option("Heading3"),
                        new Option("Heading4"),
                        new Option("Heading5")
                    };
            ListAdapter adapter = new ListAdapter(this,
                            R.layout.horizontal_checbox, weather_data);
                    listView = (ListView)findViewById(R.id.list);
                    listView.setAdapter(adapter);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }


       
    }


Download full source code Click ListWithHorizontalCheckbox


4 comments:

  1. i like your post what about i add dialogbox to check more i try it do not work my file like is here please answer mesfun@hotmail.com
    my file link is here https://www.dropbox.com/sh/kbkgl5a10lz2kzf/AADwVn6CC4PRdjSz76l2DkYva?dl=0

    ReplyDelete
  2. how to define the values of the selected check boxes for each row

    ReplyDelete
  3. How would you describe it more?

    Naomi

    ReplyDelete
  4. Thank you for taking the time to write such an informative post. Your blog is not only informative, but it is also very creative
    android app agency

    ReplyDelete