jueves, 27 de agosto de 2015

Android: Clickable text in TextView

In this case it is just a URL that can be clicked, it can be anything and do anything:
final String webURL = facilityInformation.getWeb();

        TextView web = (TextView) convertView.findViewById(R.id.website);
        Spannable span = Spannable.Factory.getInstance().newSpannable(webURL);
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURL));
                context.startActivity(browserIntent);
            }
        }, 0, webURL.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        web.setMovementMethod(LinkMovementMethod.getInstance());
        web.setText(span);

        return convertView;
Note:
- 0 and webURL.length() define the piece of the text that can be clicked.
- To set the link color you just add the following as an attribute to the TextView: "android:textColorLink="<color>""

No hay comentarios:

Publicar un comentario