Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight a TextView or LinearLayout when click it?

Tags:

android

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.

like image 391
user256239 Avatar asked Feb 02 '10 00:02

user256239


People also ask

Can a TextView be clickable?

Just like Buttons and ImageViews we can add onClickListeners to TextViews by simply adding the attribute android:onClick="myMethod" to your TextView XML tag. The other way, TextView tv = (TextView) this.

How do you declare a LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do I get TextView text?

String a = tv. getText(). toString(); int A = Integer. parseInt(a);

Which attribute is used to show any text inside TextView?

We use attribute android: hint= "Text will show here", so if we will set the TextView in the MainActivity.


3 Answers

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(...);
like image 140
Anthone Avatar answered Oct 21 '22 12:10

Anthone


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.

like image 22
Jerry Brady Avatar answered Oct 21 '22 13:10

Jerry Brady


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

like image 3
Roman Nurik Avatar answered Oct 21 '22 12:10

Roman Nurik