简体   繁体   中英

how to click on textview in android

Click on text view is not working

My xml is

<TextView
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="3"
android:gravity="center" 
android:textColor="@color/white"
android:text="click to download sevenstepstocollege"
android:textSize="15dp"
android:onClick="downloadLink"
android:clickable="true">
</TextView>

and My Activity code is

public void downloadLink(View v)
{
//String requestId = PurchasingManager.initiatePurchaseRequest(skuKye);
//String requestId=PurchasingManager.initiateItemDataRequest("DeveloperSKU-1234");
skuSet.add(skuKye);
final String requestId = PurchasingManager.initiateItemDataRequest(skuSet);
}  

But it is not working.I am not able to click that link.please guide me

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

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

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

Then, at your MainActivity.java, you add:

private TextView textview;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_main);  

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

    textview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(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?

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

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

Linkify.addLinks(topPaid, Linkify.ALL);

topPaid.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

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

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("<a href=\"http://www.google.com\">google</a> "));
    textView.setMovementMethod(LinkMovementMethod.getInstance());

In your XML as the following:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM