Thursday, 26 January 2017

ListView with Multiple CheckBox Selection Example in Android

Here is a tutorial for how to create multi checkbox selected ListView in Android by using custom adapter.

                                                                                                       
                 

In this tutorial we will learn some more things as below.

1. How to create multiple checkbox selection ListView.
2. How to create/use custom adapter for multi checkbox ListView.
3. How to read selected items from listview  in android.
4. How to get/use selected data from a custom multiple selection ListView & show it in Toast msg.


Create a new project in Android Studio & add the following files.


SplashActivity.java

public class SplashActivity extends AppCompatActivity 
{
 @Override
 protected void onCreate(Bundle savedInstanceState)
 { 
 super.onCreate(savedInstanceState);
 setContentView(R.layout.splash);
 if (getIntent().getBooleanExtra("EXIT", false)) 
 finish();
 return; 
 }

 new Handler().postDelayed(new Runnable() { @Override 

public void run() {
Intent send = new Intent(getApplicationContext(), MainActivity.class); 
 startActivity(send); } }, 4000); } }







MainActivity.java



public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {


ListView rkt_ListView;
Button btn_show_me;

private ArrayList<String> kaminey_dost_array_list = new ArrayList<String>();

private void kaminey_dost() {
kaminey_dost_array_list.add("Robi");
kaminey_dost_array_list.add("Anubhan");
kaminey_dost_array_list.add("Shikari");
kaminey_dost_array_list.add("Chittu");
kaminey_dost_array_list.add("Chuha");
kaminey_dost_array_list.add("Baba");
kaminey_dost_array_list.add("Gareeb");
kaminey_dost_array_list.add("Vinay");
kaminey_dost_array_list.add("Vakeel");
kaminey_dost_array_list.add("Gajju");
kaminey_dost_array_list.add("Ghoda");
kaminey_dost_array_list.add("Chhota Ghoda");
kaminey_dost_array_list.add("Peelu");
kaminey_dost_array_list.add("Anil");

}

RktArrayAdapter rktArrayAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
kaminey_dost();
rkt_ListView = (ListView) findViewById(R.id.rkt_listview);

rktArrayAdapter = new RktArrayAdapter(
this,
R.layout.list_row,
android.R.id.text1,
kaminey_dost_array_list
);

rkt_ListView.setAdapter(rktArrayAdapter);
rkt_ListView.setOnItemClickListener(this);

btn_show_me = (Button) findViewById(R.id.btn_show_me);
btn_show_me.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String result = "";

List<String> resultList = rktArrayAdapter.getCheckedItems();
for (int i = 0; i < resultList.size(); i++) {
result += String.valueOf(resultList.get(i)) + "\n";
}

rktArrayAdapter.getCheckedItemPositions().toString();

if (result.matches("")) {
Toast.makeText(
getApplicationContext(),
"Please select some thing from list to show",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
getApplicationContext(),
result,
Toast.LENGTH_LONG).show();
}


}
});


}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
rktArrayAdapter.rkt_toggleChecked(i);
}

public class RktArrayAdapter extends ArrayAdapter<String> {

private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();

public RktArrayAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);

for (int i = 0; i < objects.size(); i++) {
myChecked.put(i, false);
}
}

public void rkt_toggleChecked(int position) {
if (myChecked.get(position)) {
myChecked.put(position, false);
} else {
myChecked.put(position, true);
}

notifyDataSetChanged();
}

public List<Integer> getCheckedItemPositions() {
List<Integer> checkedItemPositions = new ArrayList<Integer>();

for (int i = 0; i < myChecked.size(); i++) {
if (myChecked.get(i)) {
(checkedItemPositions).add(i);
}
}

return checkedItemPositions;
}

public List<String> getCheckedItems() {
List<String> checkedItems = new ArrayList<String>();

for (int i = 0; i < myChecked.size(); i++) {
if (myChecked.get(i)) {
(checkedItems).add(kaminey_dost_array_list.get(i));
}
}

return checkedItems;
}

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

if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.list_row, parent, false);
}

CheckedTextView checked_TextView = (CheckedTextView) row.findViewById(R.id.checked_textview);
checked_TextView.setText(kaminey_dost_array_list.get(position));

Boolean checked = myChecked.get(position);
if (checked != null) {
checked_TextView.setChecked(checked);
}

return row;
}

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(MainActivity.this,
SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
}
return super.onKeyDown(keyCode, event);
}

}




splash.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"
android:background="@drawable/splash" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="170dp"
android:text="Welcome To"
android:textColor="#569B1A"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:autoLink="web"
android:text="http://androidtutorialsrkt.blogspot.in/"
android:textColor="#569B1A"
android:textSize="17sp"
android:textStyle="bold" /></RelativeLayout>


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

<ListView
android:id="@+id/rkt_listview"
android:layout_width="wrap_content"
android:layout_marginBottom="60dp"
android:layout_alignParentTop="true"
android:layout_height="wrap_content" />

<Button android:id="@+id/btn_show_me"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#2fb934"
android:textColor="#ffffff"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Show me Result"/>
</RelativeLayout>




list_row.xml



<?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="wrap_content"
android:orientation="horizontal">
<CheckedTextView
android:id="@+id/checked_textview"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"/>
</LinearLayout>

Hope this tutorial will help you. Download complete project HERE


Here is the ouput screen : 


                                               








22 comments:

  1. Webtrackker technology is the best IT training institute in NCR. Webtrackker provide training on all latest technology such as Android training. Webtrackker is not only training institute but also it also provide best IT solution to his client. Webtrackker provide training by experienced and working in the industry on same technology.Webtrackker Technology C-67 Sector-63 Noida 8802820025

    Android Training Institute In indirapuram


    Android Training Institute In Noida

    Android Training Institute In Ghaziabad


    Android Training Institute In Vaishali


    Android Training Institute In Vasundhara


    Android Training Institute In Delhi South Ex

    ReplyDelete
  2. Very Interesting Blog Post. Recently i Started Learning Android Course in NareshIT.
    Hai Author
    Can You Please Share Us Some Android Tutorials. Those are really helpful for the people like me...

    ReplyDelete
    Replies
    1. Hey Indu, Thanks for reaching to this blog. Please read all post in this blog, If need any help...Feel free to ping me here.

      Delete
  3. Yes Indu, Robi is very talented Android developer,he can really help you to get your overal development

    ReplyDelete
  4. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ..Android training in chennai with placement | Android Training in chennai |Best Android Training in chennai

    ReplyDelete
    Replies
    1. Thanks for visiting the blog.....keep visiting.

      Delete
  5. "Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.."!!
    android apps development service

    ReplyDelete
  6. Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my friends circle.
    Mobile App Development Company
    Android app Development Company
    ios app development Company
    Mobile App Development Companies

    ReplyDelete
  7. Great information..
    Im newbie, visit my blog
    Droidkenzo.blogspot.com

    ReplyDelete
  8. Hi, I read your blog this is very informative. Thanks for share this blog. You can find dedicated Web designers,Mobile Application Developers,Ecommerce Solution, SEO and marketing specialists will tailor make the solution you want for your business.Know more about click here – www.hvantagetechnologies.com

    ReplyDelete
  9. It's really informative! Thanks for the great article.
    Yberry Infosystem is a Mobile App & Web Development company, offering a wide range of services web design, web development, Mobile app Development, Mobile Website design, mobile website development, SEO , E-commerce solutions that covers almost all fields in the best possible way. Visit: www.yberryinfosystem.com

    ReplyDelete
  10. Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information
    Best Android Training in Velachery | android development course fees in chennai

    ReplyDelete
  11. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
    Android Training institute in chennai with placement | Best Android Training in velachery

    ReplyDelete
  12. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.android development course fees in chennai | android app development training in chennai

    ReplyDelete

  13. Very nice information that i have found. don't stop sharing and Please keep updating us..... Thanks

    ReplyDelete
  14. Very Nice information shared about android. Keep sharing tutorials. This blog post is really helpful.

    ReplyDelete
  15. Awesome..You have clearly explained ...Its very useful for me to know about new things..Keep on blogging..
    Android Runtime Permissions

    ReplyDelete