1

I am working with ViewGroup . I want to make a click effect on the item of the group so that User can detect which item is currently clicked. Can anyone please help me?

private View getSubCategoryListItemView(final SubCategoryItem si, double dwPercentage, final int cat_id)
    {
        LayoutInflater li = LayoutInflater.from(this);
        View vv = li.inflate(R.layout.sub_cat_list_item, llCatListHolder, false);
        ImageView ivIcon = (ImageView) vv.findViewById(R.id.iv_sub_cat_icon);
        final TextView tvName = (TextView) vv.findViewById(R.id.tv_sub_cat_name);
        ivIcon.setImageResource(AppConstants.ALL_CAT_MARKER_ICONS[cat_id-1]);
        ViewGroup.LayoutParams lpIv = ivIcon.getLayoutParams();
        lpIv.width = (int) (primaryIconWidth * dwPercentage);
        ivIcon.setLayoutParams(lpIv);
        tvName.setText(si.getSubcatHeader());
        tvName.setTextSize((float) (VIEW_WIDTH * .10 * dwPercentage));

/**************************
 *
 * This OnClickListener will be called for clicking subcategory items from the top list
 *
 * Toast.makeText(getApplicationContext(), "Entertainment ", Toast.LENGTH_SHORT).show();
 * ************************/
      //  tvName.setTextColor(Color.WHITE);

        Toast.makeText(getApplicationContext(), "BackTrack ", Toast.LENGTH_SHORT).show();

        vv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                ArrayList<SubCategoryItem> subCategoryItems;
                subCategoryItems = getSubCategoryList(cat_id);


                for(SubCategoryItem si : subCategoryItems)
                {
                //    tvName.setTextColor(Color.WHITE);
                }
                tvName.setTextColor(Color.RED);
                /*code for category*/
                /*following code will be different for each category*/
                /*category id 1 means education.
                * category id 2 means health
                * category id 3 means entertainment
                * category id 4 means government
                * category id 5 means legal
                * category id 6 means financial
                * category id 7 means job*/
            //    tvName.setTextColor(Color.WHITE);
                switch (currentCategoryID) {
                    case AppConstants.EDUCATION:
                        Toast.makeText(getApplicationContext(), "Education Entrance ", Toast.LENGTH_SHORT).show();
                        ArrayList<EducationServiceProviderItem> eduItem;
                        eduItem = constructEducationListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithEducationInfo(si.getSubcatHeader(), cat_id, eduItem);
                        break;
                    case AppConstants.HEALTH:
                        Toast.makeText(getApplicationContext(), "Helath Entrance ", Toast.LENGTH_SHORT).show();
                        //TODO write necessary codes for health
                        ArrayList<HealthServiceProviderItem> healthItem;
                        healthItem = constructHealthListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithHealthInfo(si.getSubcatHeader(), cat_id, healthItem);

                        break;


                    case AppConstants.ENTERTAINMENT:
                        tvName.setTextColor(Color.GREEN);
                        Toast.makeText(getApplicationContext(), "Entertainment ", Toast.LENGTH_SHORT).show();
                        ArrayList<EntertainmentServiceProviderItem> entItem;
                        entItem = constructEntertainmentListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithEntertainmentInfo(si.getSubcatHeader(), cat_id, entItem);
                        break;
                    //TODO write necessary codes for entertainment

                    case AppConstants.GOVERNMENT:
                        //TODO write necessary codes for government
                        break;
                    case AppConstants.LEGAL:
                        Toast.makeText(getApplicationContext(), "Legal ", Toast.LENGTH_SHORT).show();

                        ArrayList<LegalAidServiceProviderItem>legalItem;
                        legalItem = constructlegalaidListItemForHeader(cat_id,si.getSubcatHeader());
                        callMapFragmentWithLegalAidInfo(si.getSubcatHeader(),cat_id,legalItem);
                        break;
                    case AppConstants.FINANCIAL:
                        ArrayList<FinancialServiceProviderItem>financialItem;
                        financialItem = constructfinancialListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithFinancialInfo(si.getSubcatHeader(), cat_id, financialItem);
                        break;
                    case AppConstants.JOB:
                        ArrayList<JobServiceProviderItem>jobItem;
                        jobItem = constructjobListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithJobInfo(si.getSubcatHeader(), cat_id, jobItem);
                        break;
                    default:
                        break;
                }
                /*code for all*/
            }
        });
        return vv;
    }


flag

I have set an OnclickListener. But I can not detect which TextView is clicked now? I have tried to make Text color changed. Suppose I set Text color orange and commmon color black. When the first item has clicked then the text color is orange. When the another item clicked it has become also make be orange. Then two item is orange color. But previous item should be black after clicking next item and only current item should be orange.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • First post some code, next elaborate your question: which ViewGroup (Linear layout, listview etc) are you working on...and more – drWisdom Jan 22 '16 at 17:37
  • I have just wanted to make a click effect. Here I want it in linear layout. If any item in ViewGroup is clicked then it can be in different Text color or image size get large etc. I have just want to separate it from other. – Mazharul Islam Jan 22 '16 at 17:47
  • 1
    This is done programmatically. Reference your childView (Textview or button etc) by its id and set an OnClickListener to that view – drWisdom Jan 22 '16 at 17:49
  • I have set an OnclickListener. But I can not detect which TextView is clicked now? I have tried to make Text color changed. Suppose I set Text color orange and commmon color black. When the first item has clicked then the text color is orange. When the another item clicked it has become also make be orange. Then two item is orange color. But previous item should be black after clicking next item and only current item should be orange. – Mazharul Islam Jan 22 '16 at 17:57
  • Post the code that you have written so far and may be we will be better able to help – drWisdom Jan 22 '16 at 17:58
  • Ok I am going to edit my post. – Mazharul Islam Jan 22 '16 at 17:59
  • Why don't you set the OnClickListener on tv instead of vv. – drWisdom Jan 22 '16 at 18:05
  • Tidy the code a bit, move comment from OP into the question. – John Hascall Jan 24 '16 at 18:15

0 Answers0