user256239
user256239

Reputation: 18027

How to highlight a TextView or LinearLayout when click it?

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.

Upvotes: 17

Views: 24069

Answers (3)

Anthone
Anthone

Reputation: 2290

I suppose 2 Layouts

<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(...);

Upvotes: 17

Jerry Brady
Jerry Brady

Reputation: 3080

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.

Upvotes: 7

Roman Nurik
Roman Nurik

Reputation: 29745

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).

Upvotes: 3

Related Questions