0

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

Varsha
  • 49
  • 5
  • http://stackoverflow.com/questions/3328757/how-to-click-or-tap-on-a-textview-text – Nirav Ranpara Nov 21 '12 at 06:27
  • I am using a amazone apps with my current apps. In the textview the text is "click here to go to seven easy steps" – Varsha Nov 21 '12 at 06:27
  • post all your java code where you declare the downloadLink() function please ! – toantran Nov 21 '12 at 06:28
  • i am using the same page but it is not working :( – Varsha Nov 21 '12 at 06:29
  • How can i post the code here in comment?? – Varsha Nov 21 '12 at 06:30
  • String skuKye="DeveloperSKU-1234"; final Set skuSet = new HashSet(); 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); } private OnClickListener purchaseClickListener = new OnClickListener() { public void onClick(View v) { final String requestId = PurchasingManager.initiateItemDataRequest(skuSet); } }; – Varsha Nov 21 '12 at 06:35
  • android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it. – Android Nov 21 '12 at 06:48

5 Answers5

2

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?

Community
  • 1
  • 1
lcsvcn
  • 1,184
  • 3
  • 13
  • 28
1

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
}
}
mini
  • 855
  • 4
  • 15
  • 22
  • Linkify.addLinks(topPaid, Linkify.ALL); topPaid.setMovementMethod(LinkMovementMethod.getInstance()); // t1.setClickable(true); topPaid.setOnClickListener(new View.OnClickListener() { //ur logic} }); – mini Nov 21 '12 at 10:34
0
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."));
cycDroid
  • 915
  • 10
  • 16
  • i am not using the direct redirecting the website because i am using a amazone apps with my apps and i done the coding of that part..but the problem is that i am not able to click on the button – Varsha Nov 21 '12 at 06:53
  • i call the amazone api for buying the book from the user – Varsha Nov 21 '12 at 06:54
  • i have a textview and whenever i click on that textview i call the amazone apps which is integrated with my apps and the person who is using this apps he has a option to buy the book directly no need to go to amezone website directly. – Varsha Nov 21 '12 at 07:06
  • i have this code in xml. – Varsha Nov 21 '12 at 07:28
  • and i have to call this method 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); } – Varsha Nov 21 '12 at 07:28
  • i put the xml code and i have a method in java class.how to call the method with the clicking the textview in xml file – Varsha Nov 21 '12 at 07:30
0

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());
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
  • yes i do that but it is direct goes to the website but i am not using the direct redirecting the website because i am using a amazone apps with my apps and i done the coding of that part..but the problem is that i am not able to click on the button – Varsha Nov 21 '12 at 06:58
  • i cannot understand what u want to do with textview. so please add ur code for better understanding – Kirit Vaghela Nov 21 '12 at 07:04
  • i have a textview and whenever i click on that textview i call the amazone apps which is integrated with my apps and the person who is using this apps he has a option to buy the book directly no need to go to amezone website directly. – Varsha Nov 21 '12 at 07:08
  • // u can try this TextView textView = (TextView) findViewById(R.id.textView1); textView.setText(Html.fromHtml("google ")); Linkify.addLinks(textView, Linkify.ALL); textView.setOnClickListener(new OnClickListener() { public void onClick(View v) { //ur code } }); – Kirit Vaghela Nov 21 '12 at 07:10
0

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) { ... }