Question

I want to set the background color of clicked TextView included in LinearLayout. But I am unable to set the respective background. I faced not clear previous clicked background . if I clicked all then set all background color. Would you suggest me , how to set clickable TextView included in LinearLayout background.

Here is my sample code:

holder.txtName = (TextView) convertView.findViewById(R.id.row_cell_text_dummy_multilevel);
    holder.l_select = (LinearLayout) convertView.findViewById(R.id.linear_select);

 holder.txtName.setTag(position);

    holder.txtName.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                select_pos=(Integer) holder.txtName.getTag();
                    if (position==select_pos) {
                        holder.l_select.setTextColor(Color.RED);
                    }else {
                        holder.l_select.setTextColor(Color.WHITE);
                    }
                }
            });
Was it helpful?

Solution

I managed to get this much done, though you will have to do some homework and find out how to reflect the changes yourself. At the moment, the change gets reflected only when the view is scrolled.

But i hope it helps you.

1.declare a static variable that sets the position clicked on

private static int selectedPostion;

2.set the selectedPosition's value to -1 in the constructor

3.in the getView method in the onclickListener do this:

 int value = (Integer)((TextView)v).getTag();

Log.e("tag","(TextView)v).getTag() : " + value);
Log.e("tag", "position : " + position);

if(value == position) {
selectedPostion = position;
}else {
    selectedPostion = -1;
}

4.Under the onClick code entirely before return view write this:

if(selectedPostion == position) {
    view.setBackgroundColor(mContext.getResources().getColor(R.color.even_color));
    // or  holder.l_select.setTextColor(Color.RED);
}else {
view.setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
    // or holder.l_select.setTextColor(Color.WHITE);
}

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top