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 : 


                                               








23 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
  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. Great information..
    Im newbie, visit my blog
    Droidkenzo.blogspot.com

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

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

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

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

    ReplyDelete
  12. Great information.
    We are Netguru Solution India Pvt Ltd, one of the leading and developing Website Design and Mobile Application Development Company in Pune with a specialist management and expert group.
    Mobile Application Development Company In Pune
    Website Design Company In Pune

    ReplyDelete
  13. Hey bery, sorry buddy. Accidentally my server got crashed and all files has been deleted from there. I will rewrite all modules for download section once i get some free time for it.

    ReplyDelete
  14. Australia Best Tutor is one of the best Online Assignment Help providers at an affordable price. Here All Learners or Students are getting best quality assignment help with reference and styles formatting.

    Visit us for more Information

    Australia Best Tutor
    Sydney, NSW, Australia
    Call @ +61-730-407-305
    Live Chat @ https://www.australiabesttutor.com




    Our Services

    Online assignment help
    my assignment help Student
    Assignment help Student
    help with assignment Student
    Students instant assignment help

    ReplyDelete
  15. Thanks for sharing. I really liked your post, keep sharing!!

    CEH Training In Hyderbad

    ReplyDelete