user2622989
user2622989

Reputation:

OnClick() method doesn't work in TextView

I have a textView with OnClick method to navigate another activity. But, If i pressed that text it doesn't navigate. But If i used Button instead TextView, it works perfectly. Can't use OnClick method in TextView?

My XML Code;

<TextView
    android:onClick="webClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="19dp"
    android:text="Chapter 1"
    android:textSize="25dp" />

My Java Code:

public void webClick(View v) {
    Intent intent = new Intent(this, Webview.class);
    startActivity(intent);
}

Upvotes: 3

Views: 614

Answers (3)

Sanket Shah
Sanket Shah

Reputation: 4382

Just replace your code with this

<TextView
    android:id = "+@id/tvName"
    android:clickable="true"
    android:onClick="webClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="19dp"
    android:text="Chapter 1"
    android:textSize="25dp" />

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Add this attribute to textview

       android:clickable="true"

http://developer.android.com/reference/android/view/View.html#attr_android:clickable

android:clickable

Defines whether this view reacts to click events.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol clickable.

Related Methods setClickable(boolean)

Upvotes: 5

Tarsem Singh
Tarsem Singh

Reputation: 14199

try by adding android:clickable="true"

<TextView
    android:clickable="true"
    android:onClick="webClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="19dp"
    android:text="Chapter 1"
    android:textSize="25dp" />

Upvotes: 0

Related Questions