Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight the grid view item on select?

Tags:

android

I am creating a Grid view layout with image and text.I want multi-select of item which is working fine but i want to highlight the grid items which is selected.

Here is my code:

public class FragMent1 extends Fragment{

        BaseAdapter MyAdapter;
    private Context mContext;

    @SuppressLint("ValidFragment")
    public FragMent1(Context c) {
        mContext = c;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //View view = inflater.inflate(R.layout.g, null);
        View view = inflater.inflate(R.layout.gridview,null);
        final GridView listView = (GridView) view.findViewById(R.id.mainGrid);
        listView.setAdapter(new Adapter());
        //listView.setSelection(1);
        listView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub

                mode.setTitle("Select Items");
                mode.setSubtitle("One item selected");
                return true;

            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // TODO Auto-generated method stub


                int selectCount = listView.getCheckedItemCount();
                switch (selectCount) {
                case 1:
                    mode.setSubtitle("One item selected");

                    break;
                default:
                    mode.setSubtitle("" + selectCount + " items selected");

                    break;
                }

                return true;
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position,
                    long id, boolean checked) {
                // TODO Auto-generated method stub



                int selectCount = listView.getCheckedItemCount();
                switch (selectCount) {
                case 1:
                    mode.setSubtitle("One item selected");
                    break;
                default:
                    mode.setSubtitle("" + selectCount + " items selected");
                    break;
                }

            }
        });


        return view;
    }

    private class Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

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

            View myView = convertView;

            ImageView image;
            if (convertView == null) {
                image = new ImageView(FragMent1.this.getActivity());
                image.setLayoutParams(new GridView.LayoutParams(85, 85));
                image.setScaleType(ImageView.ScaleType.CENTER_CROP);



                LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
                myView = inflater.inflate(R.layout.grid_items_ontap, null);


                // Add The Image!!!           
                ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap);
                iv.setImageResource(mThumbIds[position]);


                // Add The Text!!!
                TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap);
                tv.setText(names[position] );






            }
            return myView;
        }

        private Integer[] mThumbIds = {
                R.drawable.car, R.drawable.car,
                R.drawable.car, R.drawable.car,
                R.drawable.car,R.drawable.car,R.drawable.car
        };

        private String[] names={"ab","cd","ef","gh","ij","kl","mn"};


    }
}

what should i add to the code for highlighting the grid item which will be selected.

Please suggest me some way to do it. Thanks in advance.

like image 694
user2835280 Avatar asked Oct 10 '13 09:10

user2835280


1 Answers

you can use selector to highlight item

In drawable folder create a xml file

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

    <item android:drawable="@color/blue" android:state_selected="true"/>
    <item android:drawable="@color/transparent"/>

</selector>

and set listSelector of your gridview like

android:listSelector="@drawable/list_selector"

like image 120
Kaushik Avatar answered Nov 14 '22 21:11

Kaushik