30

is it possible in android to make text view clickable if yes then how ??and if not then what will be the way for make a label clickable??i want to implement a call activity using this

private void call() {
     try {
         Intent callIntent = new Intent(Intent.ACTION_CALL);
         callIntent.setData(Uri.parse("tel:"+keywordxmlparsing.phone));
         startActivity(callIntent);
     } catch (ActivityNotFoundException activityException) {
         Log.e("dialing-example", "Call failed", activityException);
     }
 }

thanks for ur responses in advance...

atul yadav
  • 475
  • 2
  • 5
  • 9
  • possible duplicate of [How to click or tap on a TextView text](http://stackoverflow.com/questions/3328757/how-to-click-or-tap-on-a-textview-text) – Confuse Sep 05 '14 at 07:47

11 Answers11

52
textView.setOnClickListener(new View.OnClickListener());

Have you tried this?

Egor
  • 39,695
  • 10
  • 113
  • 130
18

More easier directly in the XML : with clickable = true

<TextView
                android:id="@+id/forgotPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="@string/forgotPassword"
                android:onClick="forgotPassword"
                android:clickable="true"
                />
geekInside
  • 588
  • 2
  • 8
  • 18
11

We can also get click event on TextView same as Button & ImageView.

and method is also same for all View.

like as

view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
6

First in your java file cast your TextView by xml id

TextView tv = (TextView)findViewById(R.Id.textView1);

then,

tv.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      // TODO Auto-generated method stub
   }
});
faceman
  • 1,318
  • 11
  • 20
CrazyMind
  • 1,006
  • 1
  • 20
  • 22
6

Honestly, I found a flat button to work better for what I was doing with a RecyclerView:

<Button
android:id="@+id/btnFoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"/>

Source: https://stackoverflow.com/a/30884132/2328637

Then customizing the button to fit my layout and finally adding the following to my MainActivity.java under onCreate:

Button btnFoo = (Button) findViewById(R.id.btnFoo);
btnFoo.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
      Intent intent = new Intent(MainActivity.this, FooActivity.class);
      startActivity(intent);
   }
});
Community
  • 1
  • 1
Hytek
  • 768
  • 7
  • 8
4

Try this:

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    }
});
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
user1288621
  • 99
  • 1
  • 6
4

In the xml for that TextView include the field

android:onClick="functionName"

Then in your java file associated with that xml include this function

public void functionName(View view){
// do your stuff
}
Cread Dotson
  • 982
  • 1
  • 7
  • 18
2

you can set a onclick listener to the texview like button.infact button inherits the properties from textview.

Kakey
  • 3,936
  • 5
  • 29
  • 45
0

Though it was long ago when you asked your question. But I think that the right thing to attain what you wanted is to set TextView xml attribute android:autoLink. For example:

<TextView 
    ...
    android:autoLink="phone" />
grine4ka
  • 2,878
  • 1
  • 19
  • 29
0

Simply try this one:-

Implement View.OnClickListener, then simply apply switch case and define the id of your text view in the case and pass the intent.

example:-

     @Override
 public void onClick(View v) {
    //TODO Auto-generated method stub

   switch (v.getId()) {
case R.id.textView:
    startActivity(new Intent(this,CalledClass.class));

    break;

default:
    break;
}

//here textView is id for the textView I chose.

0

TextView is also derived of View like- EditText,ListView etc.,so we can use

textView.setOnClickListener(new View.OnClickListener());