17

I have a LinearLayout that contains three TextViews. I want to highlight a TextView or the whole Layout when user clicks on the TextView. Is they any way to make it happen?

Thanks.

user256239
  • 17,717
  • 26
  • 77
  • 89

3 Answers3

17

I supperpose 2 Layout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"

    >

    <LinearLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        >
...
    </LinearLayout>
</LinearLayout>

findViewById(R.id.root).setOnClickListener(...);
Anthone
  • 2,156
  • 21
  • 26
7

I couldn't get Roman's methods working, but found my answer here instead. Use an XML resource as the background drawable and voila! it worked like a charm.

Community
  • 1
  • 1
Jerry Brady
  • 3,050
  • 24
  • 30
3

There are a number of ways of doing this.

The simplest way of doing this is by playing around with various View attributes such as android:focusable, android:focusableInTouchMode, android:clickable, and TextView attributes such as android:selectAllOnFocus.

You could also customize the appearance of your views by setting their backgrounds to StateListDrawables (a.k.a. <selector> drawables).

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82