Android Selectable Adapter

package com.resturant.vivaatech.resturantapp.Adapters;

import android.support.v7.widget.RecyclerView;
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> {
    @SuppressWarnings("unused")
    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)) {
            selectedItems.delete(position);
        } else {
            selectedItems.put(position, true);
        }
        notifyItemChanged(position);

    }

    /**
     * Clear the selection status for all items
     */
    public void clearSelection() {
        List<Integer> selection = getSelectedItems();
        selectedItems.clear();
        for (Integer i : selection) {
            notifyItemChanged(i);
        }
    }

    /**
     * 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) {
            items.add(selectedItems.keyAt(i));
        }
        return items;
    }
}
Advertisements

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(R.id.content_frame, fragment);
    ft.addToBackStack(backStateName);
    ft.commit();
  }
}


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(R.id.fragmentContainer, fragment, fragmentTag);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(backStateName);
        ft.commit();

    }
}

and used it as :

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

Reference :

http://stackoverflow.com/questions/18305945/how-to-resume-fragment-from-backstack-if-exists/18306258#18306258

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<>();
hs.addAll(al);
al.clear();
al.addAll(hs);

http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist

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 :
                                 DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
        drawer.setDrawerLockMode(lockMode);
        toggle.setDrawerIndicatorEnabled(enabled);
    }

    ...
}

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() {
@Override
public void onItemClick(View view, int position) {

clickPosition =position;

}
})
);

 

http://sapandiwakar.in/recycler-view-item-click-handler/

Android different screen size image for hdpi , mdpi , ldpi

320dp: a typical phone screen (240×320 ldpi, 320×480 mdpi, 480×800 hdpi, etc).
480dp: a tweener tablet like the Streak (480×800 mdpi).
600dp: a 7” tablet (600×1024 mdpi).
720dp: a 10” tablet (720×1280 mdpi, 800×1280 mdpi, etc).

details : http://stackoverflow.com/questions/6166677/android-screen-size-hdpi-ldpi-mdpi

Android Recycler view Horizontal and vertical Orientation Scroll Controll

mLayoutManager = new LinearLayoutManager(convertView.getContext(),LinearLayoutManager.HORIZONTAL,false);

Complete Example :


import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dsb.deshipos.Adapter.CatergoryAdapter;
import com.dsb.deshipos.Library.HandlingServer;
import com.dsb.deshipos.Model.Category;
import com.dsb.deshipos.R;

import java.util.ArrayList;

/**
* Created by dynamicsolversbangladesh1 on 3/31/16.
*/
public class ItemListFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

ArrayList categoryArrayList;

HandlingServer handlingServer;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View convertView = inflater.inflate(R.layout.itemslistfragmentlayout, container, false);

mRecyclerView = (RecyclerView) convertView.findViewById(R.id.item_recycler);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(convertView.getContext(),LinearLayoutManager.HORIZONTAL,false);
mRecyclerView.setLayoutManager(mLayoutManager);

handlingServer=new HandlingServer(convertView.getContext());
categoryArrayList=handlingServer.getAllCategories();

// specify an adapter (see also next example)
mAdapter = new CatergoryAdapter(convertView.getContext(),categoryArrayList);
mRecyclerView.setAdapter(mAdapter);
return convertView;
}
}

How to add Parent appbar to child layout in Android Material design

We have to just add

app:layout_behavior="@string/appbar_scrolling_view_behavior"

at the child layout parent layout like Relative or Linear Layout.

example:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&quot;
xmlns:app="http://schemas.android.com/apk/res-auto&quot;
xmlns:tools="http://schemas.android.com/tools&quot;
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.resturant.vivaatech.resturantapp.Activities.ReportActivity"
tools:showIn="@layout/activity_report">

Paint Style( Graphics)

Paint style এর মাঝে তিন ধরনের স্টাইল উল্লেখ যোগ্য
LinearGradient
RadialGradient
SweepGradient
তা ছাড়া Paint Style setStyle() use করেও স্টাইল চেঞ্জ করা যায় ।
Paint linePaint = new Paint();
linePaint.setStyle(Paint.Style.STROKE);
এর মাধ্যমে স্টোক দেওয়া যায়।


paintSyle.java

package com.example.graphics2d;

import android.app.Activity;
import android.os.Bundle;

public class paintStyle extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(new style(this));
}

}

style.java
package com.example.graphics2d;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.View;

public class style extends View {

public style(Context context) {
super(context);
// TODO Auto-generated constructor stub

}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);

canvas.drawColor(Color.BLACK);

Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);-----এখানে স্টোক দেওয়া হয়েছে

Typeface face=Typeface.createFromAsset(getContext().getAssets(), "chess1.ttf");

paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setTypeface(face);

canvas.drawText("WwQqRr ",20,20, paint);
canvas.drawText("WwQqRr ",20,40, paint);
canvas.drawText("WwQqRr ",20,120, paint);
canvas.drawText("WwQqRr ",20,150, paint);

paint.setColor(Color.RED);
canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight()/2, canvas.getWidth()/3, paint);

}

}four