Skip to content Skip to sidebar Skip to footer

How To Click On Textview In Android

Click on text view is not working My xml is

Solution 1:

Well, I use the following code to make a TextView clickable.

First, add this at your activity.xml, to make TextView clikable:

<TextViewandroid:id=android:id="@+id/button2" <!--idtogetthisTextView-->
    (...)
    android:onClick="onClick"  <!-- Add the function onClick() -->
    android:clickable="true"   <!-- Set the boolean clickable to true -->
/>

Then, at your MainActivity.java, you add:

privateTextView textview;    

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_main);  

    // Get the TextView by id
    textview = (TextView) findViewById(R.id.button2);

    textview.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
              // TODO when clicked on your link(TextView)
        }
    });
}

As I see now that you are trying to make a link from the TextView clickable, not just be able to click on the TextView I will let a link below to a similar question solved in Stack Overflow that you might find helpful.

The android developer reference to TextView: https://developer.android.com/reference/android/widget/TextView.html

The similar question in Stack Overflow about how to make a links in a clickable that might be helpful: How do I make links in a TextView clickable?

Solution 2:

You may use like this

<TextView
                            android:id="@+id/topPaid"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:autoLink="all"
                            android:clickable="true"                           
                            android:text="@string/topPaid"
                            android:textColor="#000000"
                            android:textColorLink="#33CC33" />

and At activity

TextViewtopPaid= (TextView) findViewById(R.id.topPaid);

Linkify.addLinks(topPaid, Linkify.ALL);

topPaid.setOnClickListener(newView.OnClickListener() {
            publicvoidonClick(View v) {

//write ur logic
}
}

Solution 3:

TextView textview = (TextView) findViewById(R.id.text);
textview.setText(Html.fromHtml("<b>Text:</b>  Text with a " + "<ahref=\"http://www.google.com\">link</a> " + "Created in the Java source code using HTML."));

Solution 4:

XML Code

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

JAVA Code

    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setText(Html.fromHtml("<ahref=\"http://www.google.com\">google</a> "));
    textView.setMovementMethod(LinkMovementMethod.getInstance());

Solution 5:

In your XML as the following:

<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:clickable="true"android:onClick="onTextViewPressed"
   />

In your attached activity public void onTextViewPressed(View view) { ... }

Post a Comment for "How To Click On Textview In Android"