简体   繁体   中英

android make links in a TextView clickable

First, I'll start by saying I've visited this two:

question 1 about this subject , question 2 about this subject and both have failed me. Second, my app is based on a single map fragment, i don't know if that's an issue, but the TextView is part of an info window which is displayed over the map.

I have the following TextView in xml:

<TextView
    android:id="@+id/tv_link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:linksClickable="true"
    android:autoLink="web" />

which I want to make into a link, I'm feeding the text programatically, and can see the text as it is, but it is not clickable, or "hyper-link"ed. The following is the part where I set up the TextView in my activity:

TextView tvLink = (TextView) v.findViewById(R.id.tv_link);
// make the link clickable
tvLink.setClickable(true);
tvLink.setMovementMethod(LinkMovementMethod.getInstance());
String text = (String) urls.get(arg0.getTitle());
// Setting the link url
tvLink.setText(Html.fromHtml(text));

I have also tried making the TextView have attribute of android:onClick="openBrowser" and have this class openBroweser:

public void openBrowser(View view){

        //Get url from tag
        String url = (String)view.getTag();

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);

        //pass the url to intent data
        intent.setData(Uri.parse(url));

        startActivity(intent);
    }

but it also didn't work. I might have made a mess while trying the different approached, but I did try to separate each try. and am confused and in need of an outside look.

EDIT 1 : added the following:

  1. My string in the res file (inside a string-array)

Click here for more info about this location

(which works here, so I assume it should be a legal link as needed)

  1. My whole XML file as requested in comment

     <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/tv_info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_link" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" /> </LinearLayout> <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" /> 

To begin with take out:

android:linksClickable="true"
android:autoLink="web"

Like this:

<TextView
    android:id="@+id/tv_link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"/>

This is working for me with a hard coded url. I suspect the way you are formatting your url is not correct.

String text = (String) urls.get(arg0.getTitle());

I'm not sure what you are getting from this, if you are using an array of strings.

The two questions you have linked to both use this format:

String text = "<a href='http://www.google.com'> Google </a>";
<a href="some site">Link text</a>

You need to format your string resources like this:

<string name="txtCredits"><a href="http://www.google.com">Google</a></string>

I cannot see how your custom class openBrowser is fetching the string in the right format for you.

I suggest you log everything.

String text = (String) urls.get(arg0.getTitle());
 Log.i("url = ", text);
tvLink.setText(Html.fromHtml(text));

What you are trying to do will not work because a Google Maps InfoWindow is not a live view.

In the documentation for Custom Info Windows it states:

The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.

and

As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view.

So the clicks on the individual tv_link TextView within the overall InfoWindow layout will not be processed.

You need to use the OnInfoWindowClickListener to listen for clicks on an InfoWindow

Use below which will do all your task

String str = holder.restContactInfoTV.getText().toString();

        int index = str.lastIndexOf(",");


        SpannableString snstr = new SpannableString(str);
        ClickableSpan Span = new ClickableSpan() {
            @Override
            public void onClick(View textView) {

                try {
                    String mobile = CUrrentOrderChild
                            .getRestaurantMobileCO();

                    Intent intent = new Intent(Intent.ACTION_CALL,
                            Uri.parse("tel:" + mobile));
                    startActivity(intent);
                } catch (Exception e) {

                }

            }

            @Override
            public void updateDrawState(TextPaint ds) {
                // TODO Auto-generated method stub
                super.updateDrawState(ds);
                // ds.setColor(Color.RED);
                 //ds.setUnderlineText(true);
            }
        };
        snstr.setSpan(Span, index + 2, snstr.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.restContactInfoTV.setText(snstr);
        holder.restContactInfoTV.setMovementMethod(LinkMovementMethod.getInstance());

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