Search code examples
androidandroid-layoutlistviewtextviewlistviewitem

How to highlight my current position in list-view using text-view tags generated dynamically on list-item click


My activity flow is this way.

**Main ListView**--- list item click

 SubListview- Listitem_1          (parent name-Group)
           Listitem_2

 childListview         Onclick Listitem_1 or 2:     
                             New ListView-new_list_item_1    (parent name-Party)
                                         -new_list_item_2

 grandchildlistview         Onclick new_list_item_1 or 2: 
                   Generate Final-List_view having n elements (parent name-Party name)

Now I have taken a linear layout which will be above list view below the toolbar.

I have created a method of headerTextviewpath that will trigger from on-item click of Listitem_1 or 2 and will generate a dynamic text-view.

Here in this method textview will be set with text of item clicked parent name

My linear layout genrated is shown below

Group > Party > Party name

now I have done this all and have also assigned color to the text-view generating dynamically.

Goal:: I want to highlight the last selected parent name in linear layout in different color then the other textview in linear layout like this

SubListview> (on main list item click)

SubListview> childListview> (Onclick Listitem_1 or 2)

SubListview> childListview> grandchildlistview (Onclick new_list_item_1 or 2)

Note: I am adding parent name as tags in textview and also storing it in arraylist namely tag_list

Code For Dynamic Textview generating:::::

method is called like this :

ArrayList tag_list=new ArrayList;

headerTextviewpath(listitem_seleted_name, functionName, true);

headerTextviewpath

    public void headerTextviewpath(final String listitem_selected, String parent_name, boolean b) {
    if (b) {
            linearLayout.removeAllViews();
        }
        final TextView tv = new TextView(this);
        tv.setLayoutParams(new 
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        //tv.setBackgroundResource(R.drawable.button_design);
        tv.setBackgroundColor(getResources().getColor(R.color.DateTheme));
        tv.setText(listitem_selected);

        Drawable next = getResources().getDrawable(R.drawable.ic_keyboard_arrow_right);
        DrawableCompat.setTint(next, ContextCompat.getColor(this, R.color.colorPos6));
        tv.setCompoundDrawablesWithIntrinsicBounds(null, null, next, null);
        tv.setTag(parnet_name);
        tv.setTextColor(getResources().getColor(R.color.colorPos3));
        tag_list.add(parnet_name);

        tv.setPadding(8, 8, 8, 8);

        linearLayout.addView(tv);

       ///using this i am getting current tag of current textview
        int childCount = linearLayout.getChildCount();
        String currentModule = tag.get(childCount - 1);

}

How can I make a logic for the above requirement please help.


Solution

  • First of all as I passed the values in the array list and also sending a flag it all notes down to a condition where my current funtion name matches the function name that is added in the array list so with that i created a simple function for it below, in the method.

    public void headerTextviewpath(final String displayText, String functionCall, boolean b) {
    
            if (b) {
                linearLayout.removeAllViews();
            }
            linearLayout.setVisibility(View.VISIBLE);
            TextView tv = new TextView(this);
            tv.setFocusable(true);
            tv.setFocusableInTouchMode(true);
            tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
    
            //tv.setBackgroundResource(R.drawable.button_design);
            tv.setBackgroundColor(getResources().getColor(R.color.DateTheme));
            tv.setText(FromHtml.getText(displayText));
    
            Drawable next = getResources().getDrawable(R.drawable.ic_keyboard_arrow_right);
            DrawableCompat.setTint(next, ContextCompat.getColor(this, R.color.colorPos6));
            tv.setCompoundDrawablesWithIntrinsicBounds(null, null, next, null);
            tv.setTag(functionCall);
            tv.setTextColor(getResources().getColor(R.color.colorPos3));
            tag.add(functionCall);
    
            tv.setPadding(8, 8, 8, 8);
    
            linearLayout.addView(tv);
    
    
            int childCount = linearLayout.getChildCount();
    
    
            Object tagObj = linearLayout.getChildAt(0).getTag();
            Log.e("TagObj", "==>" + tagObj.toString() + childCount);
    
            final String currentModule = tag.get(childCount - 1);
            //  Log.e("TagObjCurr","==>"+currentModule+" "+functionName);
            for (int i = 0; i < tag.size(); i++) {
    
                Log.e("MoudelFound==>" + i, "tag.get(i):" + tag.get(i) + "  currentModule:" + currentModule);
    
                if (tag.get(i).equals(currentModule)) {
                    tv = linearLayout.findViewWithTag(currentModule);
                    tv.setTextColor(getResources().getColor(R.color.colorPos3));
                    tv.requestFocus();
    
                } else {
    
                    tv = linearLayout.findViewWithTag(tag.get(i));
                    tv.setTextColor(getResources().getColor(R.color.colorAccent));
    
                }
            }
     }
    

    Finally it worked fine and showing the correct path as well and you can also show the color code to determine the difference.