Android Selectable Adapter

package com.resturant.vivaatech.resturantapp.Adapters;

import android.util.SparseBooleanArray;
import android.widget.Filter;

import java.util.ArrayList;
import java.util.List;

 * Created by arifh_000 on 6/14/2016.
public abstract class SelectableAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
    private static final String TAG = SelectableAdapter.class.getSimpleName();

    private SparseBooleanArray selectedItems;

    public SelectableAdapter () {
        selectedItems = new SparseBooleanArray ();

     * Indicates if the item at position position is selected
     * @param position Position of the item to check
     * @return true if the item is selected, false otherwise
    public boolean isSelected(int position) {
        return getSelectedItems().contains(position);

     * Toggle the selection status of the item at a given position
     * @param position Position of the item to toggle the selection status for
    public void toggleSelection(int position) {
        if (selectedItems.get(position, false)) {
        } else {
            selectedItems.put(position, true);


     * Clear the selection status for all items
    public void clearSelection() {
        List<Integer> selection = getSelectedItems();
        for (Integer i : selection) {

     * Count the selected items
     * @return Selected items count
    public int getSelectedItemCount() {
        return selectedItems.size();

     * Indicates the list of selected items
     * @return List of selected items ids
    public List<Integer> getSelectedItems() {
        List<Integer> items = new ArrayList<>(selectedItems.size());
        for (int i = 0; i < selectedItems.size(); ++i) {
        return items;

How to prevent fragments from multiple instance

private void replaceFragment (Fragment fragment){
  String backStateName = fragment.getClass().getName();

  FragmentManager manager = getSupportFragmentManager();
  boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);

  if (!fragmentPopped){ //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(, fragment);

For my case:
I have declared it in  a class 

public static void replaceFragment (FragmentManager manager,Fragment fragment){

    String backStateName =  fragment.getClass().getName();
    String fragmentTag = backStateName;
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
    if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) { //fragment not in back stack, create it.
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(, fragment, fragmentTag);


and used it as :

Functions.replaceFragment(getFragmentManager(),new OrderFoodTypeFragment());

Reference :

How to Add customize Android DialogFragment

public class ColorDialogFragment extends SherlockDialogFragment {

    public ColorDialogFragment() {
    //You need to provide a default constructor

    public View onCreateView(LayoutInflater inflater, 
                     ViewGroup container,
                     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_color_picker, container);
    // R.layout.dialog_color_picker is the custom layout of my dialog
    WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
    wmlp.gravity = Gravity.LEFT;
    return view;

    public void onCreate(Bundle savedInstanceState) {
    // this setStyle is VERY important.
    // STYLE_NO_FRAME means that I will provide my own layout and style for the whole dialog
    // so for example the size of the default dialog will not get in my way
    // the style extends the default one. see bellow.        


<style name="colorPickerStyle" parent="Theme.Sherlock.Light.Dialog">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
i Have done it as :

<style name="colorPickerStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:cacheColorHint">@android:color/white</item>
    <item name="android:windowBackground">@android:color/white</item>

How to convert ArrayList to HashSet for eliminating duplicates


List<String> al = new ArrayList<>();
// add elements to al, including duplicates
Set<String> hs = new HashSet<>();

How to Send data from one Fragment to another fragment in android

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
Bundle has put methods for lots of data types. See this

Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:

Bundle bundle = this.getArguments();
int myInt = bundle.getInt(key, defaultValue);

How to Disable Navigation Drawer for specific fragments

1 . Create an interface first

public interface DrawerLocker {
    public void setDrawerEnabled(boolean enabled);

2. Implement interface on Activity that’s has navigation drawer / Container Activity

public class MainActivity extends Activity implements DrawerLocker {

    public void setDrawerEnabled(boolean enabled) {
        int lockMode = enabled ? DrawerLayout.LOCK_MODE_UNLOCKED :


3. Cast that interface on you specific fragments

((DrawerLocker) getActivity()).setDrawerEnabled(false);


RecyclerView OnTouch Listener

1. implements RecyclerView.OnItemTouchListener
2. final GestureDetector mGestureDetector = new GestureDetector(getActivity().getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {

@Override public boolean onSingleTapUp(MotionEvent e) {
return true;


3. mRecyclerView.addOnItemTouchListener(
new OrderFoodTypeFragment(getActivity().getApplicationContext(), new OrderFoodTypeFragment.OnItemClickListener() {
public void onItemClick(View view, int position) {

clickPosition =position;