How to click or tap on a TextView text

AndroidOnclickListenerTextview

Android Problem Overview


I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.

I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.

Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?

Android Solutions


Solution 1 - Android

You can set the click handler in xml with these attribute:

android:onClick="onClick"
android:clickable="true"

Don't forget the clickable attribute, without it, the click handler isn't called.

main.xml

    ...

    <TextView 
       android:id="@+id/click"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"				  
       android:text="Click Me"
       android:textSize="55sp"
       android:onClick="onClick"	    		
       android:clickable="true"/>
    ...

MyActivity.java

       public class MyActivity extends Activity {
    
          public void onClick(View v) {
            ...
          }  
       }

Solution 2 - Android

This may not be quite what you are looking for but this is what worked for what I'm doing. All of this is after my onCreate:

boilingpointK = (TextView) findViewById(R.id.boilingpointK);

boilingpointK.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if ("Boiling Point K".equals(boilingpointK.getText().toString()))
            boilingpointK.setText("2792");
        else if ("2792".equals(boilingpointK.getText().toString()))
            boilingpointK.setText("Boiling Point K");
    }
});

Solution 3 - Android

OK I have answered my own question (but is it the best way?)

This is how to run a method when you click or tap on some text in a TextView:

package com.textviewy;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class TextyView extends Activity implements OnClickListener {

TextView t ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    t = (TextView)findViewById(R.id.TextView01);
    t.setOnClickListener(this);
}

public void onClick(View arg0) {
	t.setText("My text on click");  
    }
}

and my main.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content"             android:layout_height="wrap_content"></LinearLayout>
<ListView android:id="@+id/ListView01" android:layout_width="wrap_content"   android:layout_height="wrap_content"></ListView>
<LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content"   android:layout_height="wrap_content"></LinearLayout>

<TextView android:text="This is my first text"
 android:id="@+id/TextView01" 
 android:layout_width="wrap_content" 
 android:textStyle="bold"
 android:textSize="28dip"
 android:editable = "true"
 android:clickable="true"
 android:layout_height="wrap_content">
 </TextView>
 </LinearLayout>

Solution 4 - Android

from inside an activity that calls a layout and a textview, this click listener works:

setContentView(R.layout.your_layout);
TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
String TAG = "yourLogCatTag";
tvGmail.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View viewIn) {
				try {
					Log.d(TAG,"GMAIL account selected");
				} catch (Exception except) {
					Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
				}
			}
        });

the text view is declared like this (default wizard):

		<TextView
		    android:id="@+id/tvGmail"
		    android:layout_width="fill_parent"
		    android:layout_height="wrap_content"
		    android:text="@string/menu_id_google"
		    android:textSize="30sp" />

and in the strings.xml file

<string name="menu_id_google">Google ID (Gmail)</string>

Solution 5 - Android

Although you can resolve the problem by setting the listener to textview, it's recommended not to. You should use flat button as it is a subclass of Button and it provides many attributes which TextView doesn't.


To use flat button, add style="?android:attr/borderlessButtonStyle" attribute -

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DONE"
    style="?android:attr/borderlessButtonStyle"/>

Solution 6 - Android

in textView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:onClick="onClick"
    android:clickable="true"

You must also implement View.OnClickListener and in On Click method can use intent

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
           intent.setData(Uri.parse("https://youraddress.com"));    
            startActivity(intent);

I tested this solution works fine.

Solution 7 - Android

To click on a piece of the text (not the whole TextView), you can use Html or Linkify (both create links that open urls, though, not a callback in the app).

Linkify

Use a string resource like:

<string name="links">Here is a link: http://www.stackoverflow.com</string>

Then in a textview:

TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);

Html

Using Html.fromHtml:

<string name="html">Here you can put html &lt;a href="http://www.stackoverflow.com"&gt;Link!&lt;/&gt;</string>

Then in your textview:

textView.setText(Html.fromHtml(getString(R.string.html)));

Solution 8 - Android

You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).

holder.bt_foo_ex.addTextChangedListener(new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// code during!
			
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// code before!
			
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			// code after!
			
		}
	});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDroidView Question on Stackoverflow
Solution 1 - AndroidPatrick CullenView Answer on Stackoverflow
Solution 2 - AndroidJoshua SutherlandView Answer on Stackoverflow
Solution 3 - AndroidDroidView Answer on Stackoverflow
Solution 4 - Androidtony gilView Answer on Stackoverflow
Solution 5 - AndroidConfuseView Answer on Stackoverflow
Solution 6 - Androidrp23bView Answer on Stackoverflow
Solution 7 - Androidnjzk2View Answer on Stackoverflow
Solution 8 - AndroidMarcelo AmaralView Answer on Stackoverflow