Tuesday, 13 February 2018

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 item on scroll using endless recyclerview 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:recyclerview-v7:26.+'
     compile 'com.android.support:cardview-v7:26.+'
  • activity_main.xml 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical"

    tools:context="com.example.abc.endlessrecyclerview.MainActivity">



    <android.support.v7.widget.RecyclerView

        android:id="@+id/recyclerView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">




    </android.support.v7.widget.RecyclerView>

    <ProgressBar
        android:id="@+id/progBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:visibility="gone" />

</RelativeLayout>
  • 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:orientation="vertical"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" app:cardElevation="10dp" app:cardPreventCornerOverlap="true" card_view:contentPadding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_Rollno" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/colorPrimaryDark" android:textSize="30dp" android:textStyle="bold" /> <TextView android:id="@+id/tv_Name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:textColor="@color/DarkRed" android:textSize="25dp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>

  • The Above Code was use to create XML file..
  • After Create   main Java class and  POJO class


  • MainActivity.java
      public class MainActivity extends AppCompatActivity { private ArrayList<StudentInfo> studentInfoArrayList; private Adapter adapter; ProgressBar progressBar; RecyclerView recyclerView; LinearLayoutManager linearLayoutManager; private boolean userScrolled = true; int pastVisiblesItems, visibleItemCount, totalItemCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); progressBar = (ProgressBar) findViewById(R.id.progBar); linearLayoutManager = new LinearLayoutManager(MainActivity.this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); studentInfoArrayList = new ArrayList<StudentInfo>(); StudentInfo studentInfo = new StudentInfo(); studentInfo.setName("Mahesh"); studentInfo.setRollNo("Rollno = 1"); studentInfoArrayList.add(studentInfo); studentInfo = new StudentInfo(); studentInfo.setName("Axay"); studentInfo.setRollNo("Rollno = 2"); studentInfoArrayList.add(studentInfo); studentInfo = new StudentInfo(); studentInfo.setName("Vijay"); studentInfo.setRollNo("Rollno = 3"); studentInfoArrayList.add(studentInfo); studentInfo = new StudentInfo(); studentInfo.setName("Druv"); studentInfo.setRollNo("Rollno = 4"); studentInfoArrayList.add(studentInfo); studentInfo = new StudentInfo(); studentInfo.setName("Prakash "); studentInfo.setRollNo("Rollno = 5"); studentInfoArrayList.add(studentInfo); studentInfo = new StudentInfo(); studentInfo.setName("Vishal"); studentInfo.setRollNo("Rollno = 6"); studentInfoArrayList.add(studentInfo); adapter = new Adapter(studentInfoArrayList, getApplicationContext()); recyclerView.setAdapter(adapter); adapter.notifyDataSetChanged(); implementScrollListener(); } private void implementScrollListener() { recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { userScrolled = true; } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); visibleItemCount = linearLayoutManager.getChildCount(); totalItemCount = linearLayoutManager.getItemCount(); pastVisiblesItems = linearLayoutManager .findFirstVisibleItemPosition(); if (userScrolled && (visibleItemCount + pastVisiblesItems) == totalItemCount) { userScrolled = false; addDataToList(); } } }); } private void addDataToList() { progressBar.setVisibility(View.VISIBLE); new Handler().postDelayed(new Runnable() { @Override public void run() { for (int i = 0; i <= 5; i++) { studentInfoArrayList.add(studentInfoArrayList.get(i)); } adapter.notifyDataSetChanged(); Toast.makeText(getApplicationContext(), "Data Updated.", Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); } }, 4000); } }
  • The Above mainactivity class i have a used the ArrayList.
  • I have used onScrollStateChanged and onScrolled method.
  • I have used the Adapter class....
  • Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> { ArrayList<StudentInfo> mStudentInfoArrayList; Context mContext; public Adapter(ArrayList<StudentInfo> studentInfoArrayList, Context applicationContext) { mStudentInfoArrayList = studentInfoArrayList; mContext = applicationContext; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { final StudentInfo studentInfo = mStudentInfoArrayList.get(position); holder.tv_Name.setText(studentInfo.getName()); holder.tv_RollNo.setText(studentInfo.getRollNo()); } @Override public int getItemCount() { return mStudentInfoArrayList.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { TextView tv_Name,tv_RollNo; public MyViewHolder(View itemView) { super(itemView); tv_Name = (TextView) itemView.findViewById(R.id.tv_Name); tv_RollNo = (TextView) itemView.findViewById(R.id.tv_Rollno); } } }
  • StudentInfo.java
    class StudentInfo {
    
        String Name;
        String RollNo;
    
        public StudentInfo() {
        }
    
        public String getName() {
            return Name;
        }
    
        public void setName(String name) {
            this.Name = name;
        }
    
        public String getRollNo() {
            return RollNo;
        }
    
        public void setRollNo(String rollNo) {
            RollNo = rollNo;
        }
    
    }
    
    
  • The Above StudentInfo.java class was call by POJO class
  • In this class i have used the getter & Setter method .
  • This method use in mainactivity and adapter class to set & get Student details.
Let's Try This Code.....
Download Full Source Code.
EndlessRecyclerView.zip





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





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...