'ListView: textIsSelectable and onItemClick

Context

I have a list view where the row is basically composed of two TextView (a title and a content).

The second TextView can have a long text so I set maxLines="6". When user click on the row I get rid of the maxLines in order to show the full text.

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        TextView content = (TextView ) view.findViewById(R.id.content);

        int limit = getResources().getInteger(R.integer.default_max_lines);

        if (content.getMaxLines() > limit) {
            content.setMaxLines(limit);
        }
        else {
            content.setMaxLines(Integer.MAX_VALUE);
        }
    }

Problem

Above code works great. I would like to be able to select my second TextView (content) too so I set android:textIsSelectable="true" (also tried setting programmatically).

But I can't expand/collapse my TextView because onItemClick is no longer called

That's because textIsSelectable catches all click event... From Android doc:

When you call this method to set the value of textIsSelectable, it sets the flags focusable, focusableInTouchMode, clickable, and longClickable to the same value. These flags correspond to the attributes android:focusable, android:focusableInTouchMode, android:clickable, and android:longClickable. To restore any of these flags to a state you had set previously, call one or more of the following methods: setFocusable(), setFocusableInTouchMode(), setClickable() or setLongClickable().

I tried to set these flags to false after setTextIsSelectable(true) but I didn't manage to make it work.

So, any ideas how to use both textIsSelectable and onItemClick?

PS: Supporting only Android > 4.0.



Solution 1:[1]

I stumbled to the same problem and didn't fine any answer to this now 8 years old question. My solution was as follows:

  1. Set android:textIsSelectable="true" in the TextView
  2. Don't set onItemClickListener to the ListView
  3. Set OnClickListener in getView of the adapter to the TextView:
    override fun getView(position: Int, view: View?, parent: ViewGroup): View {
        ...
        val listener = View.OnClickListener {
            ...
        }
        viewHolder.textView.setOnClickListener(listener)
    }

That enabled click on the TextView as well as selection of its text content.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Juha