Sunday, August 14, 2016

Multiple Selection in ListView using Contextual Action Bar


Hi .... Dear All ....  Today I am uploading a superb example of how select multiple items in ListView using Contextual Action Bar and take action on selected items. 

The action bar is a dedicated piece of real estate at the top of each screen that is generally persistent throughout the app.
It provides several key functions:
  • Makes important actions prominent and accessible in a predictable way (such as New or Search).
  • Supports consistent navigation and view switching within apps.
  • Reduces clutter by providing an action overflow for rarely used actions.
  • Provides a dedicated space for giving your app an identity.
Contextual Action Bars
  • contextual action bar (CAB) is a temporary action bar that overlays the app's action bar for the duration of a particular sub-task. CABs are most typically used for tasks that involve acting on selected data or text.
From here the user can:
  • Select additional elements by touching them.
  • Trigger an action from the CAB that applies to all selected data items. The CAB then automatically dismisses itself.
  • Dismiss the CAB via the navigation bar's Back button or the CAB's checkmark button. This removes the CAB along with all selection highlights.
  • Use CABs whenever you allow the user to select data via long press. You can control the action content of a CAB in order to insert the actions you would like the user to be able to perform.

In this example you can set alarm after a specified time interval in secs. After your specified time interval your device will vibrate.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box

1. Output Will be like 


2. XML Files 
a. Create a xml file in menu folder 
     i had created context_menu.xml in menu folder which is as 


<?xml version="1.0" encoding="utf-8"?>

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

    <item        android:id="@+id/delete"

        android:title="Delete Items"

        android:icon="@mipmap/ic_launcher"        />
</menu>

b. activity_main.xml is as 

<?xml version="1.0" encoding="utf-8"?>

<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="com.exam.ravi.listviewcontextmenu.MainActivity">

    <ListView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/listView"
/>
</RelativeLayout>

3. Java Files is as MainActivity.java 

package com.exam.ravi.listviewcontextmenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    ArrayAdapter<String> adapter;
    ArrayList<String> list = new ArrayList<>();
    ArrayList<String> list_items = new ArrayList<>();
    int count =0;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        list.add("Android");
        list.add("iPhone");
        list.add("Java");
       list.add("Python");
       list.add("Ruby");
       list.add("PHP");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
{
            @Override

            public void onItemCheckedStateChanged(ActionMode mode, int position,
                                       long id, boolean checked) {
                count++;
                mode.setTitle(count+"Items Selected");
                list_items.add(list.get(position));
            }

            @Override

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.context_menu, menu);
                return true;
            }

            @Override

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch(item.getItemId())
                {
                    case R.id.delete:
                        for(String msg:list_items)
                        {
                            adapter.remove(msg);
                        }
                        Toast.makeText(getBaseContext(),count+"Removed",
                            Toast.LENGTH_LONG).show();
                        count=0;
                        mode.finish();
                        return true;
                    default:
                        return false;
                }

            }

            @Override

            public void onDestroyActionMode(ActionMode mode) {

            }
        });
    }
}

No comments:

Post a Comment