atul yadav
atul yadav

Reputation: 475

how to make text view clickable in android?

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

Upvotes: 30

Views: 104815

Answers (11)

Niranj Patel
Niranj Patel

Reputation: 33238

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

            }
        });

Upvotes: 12

Amandeep Sharma
Amandeep Sharma

Reputation: 1

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

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

Upvotes: 0

Cread Dotson
Cread Dotson

Reputation: 1092

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
}

Upvotes: 4

Parv Bhardwaj
Parv Bhardwaj

Reputation: 1

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.

Upvotes: 0

Hytek
Hytek

Reputation: 758

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);
   }
});

Upvotes: 6

geekInside
geekInside

Reputation: 588

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"
                />

Upvotes: 18

grine4ka
grine4ka

Reputation: 2928

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" />

Upvotes: 0

Nitin_Narale
Nitin_Narale

Reputation: 1046

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
   }
});

Upvotes: 6

user1288621
user1288621

Reputation: 99

Try this:

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

    }
});

Upvotes: 4

Kakey
Kakey

Reputation: 4024

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

Upvotes: 2

Egor
Egor

Reputation: 40193

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

Have you tried this?

Upvotes: 52

Related Questions