Saturday, 25 November 2017

Custom Listview With Check-box

              Custom Listview With Check-box

Objective

The main objective of this post is to help you understand how to make custom list view in your android Application.

You will get Final Output:




  • In This activity you will Crate  XML file.
  • First implement below Card view library in app:Build.gradle
        compile 'com.android.support:cardview-v7:26.+'
  • activity_main.xml 
    <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.oves.customlistview2.MainActivity">
    <TextView       
         android:layout_width="match_parent"       
         android:layout_height="wrap_content"        
         android:background="@color/colorAccent1"        
         android:gravity="center"       
         android:text="Students List"       
         android:textColor="#942d2a"       
         android:textSize="40dp"        
         android:textStyle="bold" />
    
       <ListView  
          android:id="@+id/listview"       
          android:layout_width="match_parent"       
          android:layout_height="match_parent">
    
       </ListView>
    
    </LinearLayout>
    
  • activity_result.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Students Details" android:textColor="#942d2a" android:textSize="40dp" android:textStyle="bold" /> <android.support.v7.widget.CardView android:layout_width="130dp" android:layout_height="130dp" android:layout_gravity="center" app:cardElevation="10dp" app:cardPreventCornerOverlap="true" card_view:contentPadding="10dp"> <ImageView android:id="@+id/img" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="5dp" app:cardElevation="10dp" app:cardPreventCornerOverlap="true" card_view:contentPadding="10dp"> <TextView android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:ellipsize="end" android:gravity="center" android:textColor="@color/colorAccent" android:textSize="35dp" android:textStyle="bold" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="5dp" app:cardElevation="10dp" app:cardPreventCornerOverlap="true" card_view:contentPadding="10dp"> <TextView android:id="@+id/tvRollno" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:ellipsize="end" android:gravity="center" android:textColor="@color/colorPrimaryDark" android:textSize="35dp" android:textStyle="bold" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="5dp" app:cardElevation="10dp" app:cardPreventCornerOverlap="true" card_view:contentPadding="10dp"> <TextView android:id="@+id/tvAdress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:ellipsize="end" android:gravity="center" android:textSize="25dp" android:textColor="@color/colorPrimaryDark" android:textStyle="bold" /> </android.support.v7.widget.CardView> </LinearLayout> </ScrollView> </LinearLayout>
    
    
  • list_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="10dp" app:cardElevation="10dp" app:cardBackgroundColor="@color/colorAccent" app:cardPreventCornerOverlap="true" card_view:contentPadding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/selector" android:orientation="horizontal"> <ImageView android:id="@+id/img" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_margin="10dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="1dp" android:layout_height="match_parent" android:background="#fff" android:orientation="horizontal" /> <LinearLayout android:layout_width="180dp" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView_name" android:layout_width="180dp" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:layout_weight="1" android:gravity="center" android:hint="Name" android:textSize="15dp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="1dp" android:layout_height="match_parent" android:background="#fff" android:orientation="horizontal" /> <LinearLayout android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <CheckBox android:id="@+id/chechbox1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Android" /> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>
  • The following Code was use to create XML file..
  • After Create   main Java class and  POJO class.
  • MainActivity.java
public class MainActivity extends AppCompatActivity {
    ListView listView;
    private ArrayList<StudentInfo> mStudentInfoArrayList;
    Context mContext;

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

        mStudentInfoArrayList = new ArrayList<>();
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setName("Mohammad");
        studentInfo.setImage(R.drawable.rsz_mohammad);
        studentInfo.setRollno("Rollno = 1");
        studentInfo.setAddress("Address\n" +
                "Village : Meta\n" +
                "Taluka : Vadgam\n" +
                "Dist : Banaskantha");
        mStudentInfoArrayList.add(studentInfo);

        studentInfo = new StudentInfo();
        studentInfo.setName("Vasim");
        studentInfo.setImage(R.drawable.rsz_wasim);
        studentInfo.setRollno("Rollno = 2");
        studentInfo.setAddress("Address\n" +
                "Village : Ilol\n" +
                "Taluka : Vadgam\n" +
                "Dist : Banaskantha");
        mStudentInfoArrayList.add(studentInfo);

        studentInfo = new StudentInfo();
        studentInfo.setName("Ovesh");
        studentInfo.setImage(R.drawable.rsz_shabbir);
        studentInfo.setRollno("Rollno = 3");
        studentInfo.setAddress("Address\n" +
                "Village : Sedrana\n" +
                "Taluka : Vadgam\n" +
                "Dist : Banaskantha");
        mStudentInfoArrayList.add(studentInfo);


        studentInfo = new StudentInfo();
        studentInfo.setName("Ahmad");
        studentInfo.setImage(R.drawable.rsz_ahemad);
        studentInfo.setRollno("Rollno = 4");
        studentInfo.setAddress("Address\n" +
                "Village : Vaso\n" +
                "Taluka : Vadgam\n" +
                "Dist : Banaskantha");
        mStudentInfoArrayList.add(studentInfo);

        studentInfo = new StudentInfo();
        studentInfo.setName("Kamiyab");
        studentInfo.setImage(R.drawable.rsz_kamiyab);
        studentInfo.setRollno("Rollno = 5");
        studentInfo.setAddress("Address\n" +
                "Village : Kakoshi\n" +
                "Taluka : Vadgam\n" +
                "Dist : Banaskantha");
        mStudentInfoArrayList.add(studentInfo);

        studentInfo = new StudentInfo();
        studentInfo.setName("Shabbir");
        studentInfo.setImage(R.drawable.rsz_mohammad);
        studentInfo.setRollno("Rollno = 6");
        studentInfo.setAddress("Address\n" +
                "Village : Haiderpura\n" +
                "Taluka : Vadgam\n" +
                "Dist : Banaskantha");
        mStudentInfoArrayList.add(studentInfo);

        studentInfo = new StudentInfo();
        studentInfo.setName("Mohsin");
        studentInfo.setImage(R.drawable.rsz_mohsin);
        studentInfo.setRollno("Rollno = 7");
        studentInfo.setAddress("Address\n" +
                "Village : Badergadh\n" +
                "Taluka : Vadgam\n" +
                "Dist : Banaskantha");
        mStudentInfoArrayList.add(studentInfo);


        listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(new MainActivity.NameAdapter(this, mStudentInfoArrayList));

    }

    class NameAdapter extends BaseAdapter {
        private ArrayList<StudentInfo> mStudentInfoArrayLists;

        Context mContext;
        private LayoutInflater inflater = null;


        public NameAdapter(MainActivity mainActivity, ArrayList<StudentInfo> studentList) {
            mContext = mainActivity;
            mStudentInfoArrayLists = studentList;
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }


        @Override       
            public int getCount() {
            return mStudentInfoArrayList.size();
        }

        @Override       
            public Object getItem(int position) {
            return position;
        }

        @Override       
            public long getItemId(int position) {
            return position;
        }


        @Override       
            public View getView(final int position, View convertView, ViewGroup parent) {
            final StudentInfo studentInfo = mStudentInfoArrayLists.get(position);
            View view = convertView;
            final NameHolder holder;




            if (view == null) {
                view = inflater.inflate(R.layout.list_item,parent,false);
                holder = new NameHolder();
                holder.tv = (TextView) view.findViewById(R.id.textView_name);
                holder.iv = (ImageView) view.findViewById(R.id.img);
                holder.checkBox1 = (CheckBox) view.findViewById(R.id.chechbox1);

                view.setTag(holder);

            } else {
                holder = (NameHolder) view.getTag();
            }


            holder.tv.setText(mStudentInfoArrayList.get(position).getName());
            holder.iv.setImageResource(mStudentInfoArrayList.get(position).getImage());
            holder.checkBox1.setChecked(mStudentInfoArrayList.get(position).isSelected());
            holder.checkBox1.setTag(mStudentInfoArrayList.get(position));

            holder.checkBox1.setOnClickListener(new View.OnClickListener() {
                @Override               
                    public void onClick(View v) {

                    CheckBox cb = (CheckBox) v;
                    StudentInfo studentInfo = (StudentInfo) cb.getTag();
                    studentInfo.setIsSelected(cb.isChecked());
                    notifyDataSetChanged();
                }
            });

                holder.tv.setOnClickListener(new View.OnClickListener() {
                    @Override                   
                        public void onClick(View view) {

                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        intent.putExtra("studentlist", studentInfo);
                        startActivity(intent);

                    }
                });



            return view;
        }


    }

    private class NameHolder {
        TextView tv;
        ImageView iv;
        CheckBox checkBox1;

    }
}
  • The Above Code i have a use the ArrayList.
  • I have a use the Adapter class....in innerClass
  • StudentInfo.java

class StudentInfo implements Parcelable {

    public StudentInfo() {

    }

    private String Name;
    private int Image;
    private String Rollno;

    protected StudentInfo(Parcel in) {
        Name = in.readString();
        Image = in.readInt();
        Rollno = in.readString();
        Address = in.readString();
        isSelected = in.readByte() != 0;
    }

    @Override    
        public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(Name);
        dest.writeInt(Image);
        dest.writeString(Rollno);
        dest.writeString(Address);
        dest.writeByte((byte) (isSelected ? 1 : 0));
    }

    @Override   
        public int describeContents() {
        return 0;
    }

    public static final Creator<StudentInfo> CREATOR = new Creator<StudentInfo>() {
        @Override        
            public StudentInfo createFromParcel(Parcel in) {
            return new StudentInfo(in);
        }

        @Override        
            public StudentInfo[] newArray(int size) {
            return new StudentInfo[size];
        }
    };

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getImage() {
        return Image;
    }

    public void setImage(int image) {
        Image = image;
    }

    public String getRollno() {
        return Rollno;
    }

    public void setRollno(String rollno) {
        Rollno = rollno;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    public boolean isSelected() {
        return isSelected;
    }


    private  String Address;
    private boolean isSelected;


    public void setIsSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }
}
  • The Above StudentInfo.java class was call by POJO class
  • In this class use the getter & Setter method .
  • ResultActivity.java
public class ResultActivity extends AppCompatActivity {

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

        TextView textView = (TextView) findViewById(R.id.tvAdress);
        TextView textView1 = (TextView) findViewById(R.id.tvRollno);
        TextView textView2 = (TextView) findViewById(R.id.tvName);
        ImageView imageView = (ImageView) findViewById(R.id.img);

        StudentInfo stud = getIntent().getParcelableExtra("studentlist");

        imageView.setImageResource(stud.getImage());
        textView.setText(stud.getAddress());
        textView1.setText(stud.getRollno());
        textView2.setText(stud.getName());


    }
}
Let's Try This Code.....
Download Full Source Code
CustomListView.zip





No comments:

Post a Comment

Load More Items On Scroll Android (Endless Scrolling)

              LOAD MORE ITEM ON SCROLL Objective The main objective of this post is to help you understand how to load more ite...