help with creating links TextView, please tried many options, no help, java.lang.NullPointerException error

I tried these options:

1) add a listener

texttest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Uri adress= Uri.parse("www.google.ru"); Intent browser= new Intent(Intent.ACTION_VIEW, adress); startActivity(browser); } }); 

2)

 TextView clickableTextLink = (TextView)findViewById(R.id.textView_About); clickableTextLink.setMovementMethod(LinkMovementMethod.getInstance()); 

3) This method works, but you cannot create a link of the form.

<a href="google.ru.ru">Google</a>

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Адрес: google.ru" android:autoLink="web" android:linksClickable="true" android:textSize="14dp" android:id="@+id/texttest" /> 

3 answers 3

A difficult way to not only make links clickable, but also to catch clicking on them:

  1. We take the usual TextView :

     <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14dp" android:id="@+id/texttest" /> 
  2. Create a class to handle clicks on links in the text:

     public class MakeLinksClicable { private final static String LOG = MakeLinksClicable.class.getSimpleName(); public static class CustomerTextClick extends ClickableSpan { String mUrl; public CustomerTextClick(String url) { mUrl = url; } @Override public void onClick(View widget) { //Тут можно как-то обработать нажатие на ссылку //Сейчас же мы просто открываем браузер с ней Log.i(LOG, "url clicked: " + this.mUrl); Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(mUrl)); widget.getContext().startActivity(i); } } public static SpannableStringBuilder reformatText(CharSequence text) { int end = text.length(); Spannable sp = (Spannable) text; URLSpan[] urls = sp.getSpans(0, end, URLSpan.class); SpannableStringBuilder style = new SpannableStringBuilder(text); for (URLSpan url : urls) { style.removeSpan(url); MakeLinksClicable.CustomerTextClick click = new MakeLinksClicable.CustomerTextClick(url.getURL()); style.setSpan(click, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return style; } } 
  3. We assign a TextView text, indicating to it that it is in HTML format and clickable and catch clicks on the links using the class from the previous paragraph:

     TextView textView = ... //находим TextView //Экранируем кавычки в атрибуте html тега слэшем: String textWithLink = "<a href=\"http://www.google.com\">Google</a>"; //Указываем с помощью Html.fromHtml, что у нас не просто текст: textView.setText(Html.fromHtml(textWithLink, null, null)); ////Указываем что разрешаем ссылки кликать: textView.setLinksClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance()); //Научаемся отлавливать клики пропустив текст через наш класс из пред. пункта. CharSequence text = textView.getText(); if (text instanceof Spannable) { textView.setText(MakeLinksClicable.reformatText(text)); } 

Total:

The link in the TextView will be colored with the default color of the link selection, and when you click on it, the address will be displayed in the logs and the browser will start with this link.

UPD_0:

  1. To add a question - click "edit" - see the screenshot.

enter image description here

  1. If the problem stated in the question is resolved - the question must be noted true by clicking on the "tick" to the left of the question body.

  2. If in the process of solving a new question is born, ask a new question in a separate post on the site. The meaning of the site: one question - one answer.

  • I did as you said, but the error remained ( - java
  • @java, what a mistake and where? - YurySPb
  • as I understand you in the textView code, what you have is mytextView and textToSet is string example = "<a href=\" google.com \"> Google </a>"; Yes? - java
  • Here is the java.lang.RuntimeException error: Unable to start activity ComponentInfo {ru.by_em.gradecalculator, / ru.by_em.gradecalculator.MainActivity}: java.lang.NullPointerException, swears at the line mytextView.setText (Html.fromHtml, textWithLinkr.c.nullPointerException, swears at the line mytextView.setText (Html.fromHtml, textWithLinkr.c.hull.fointerException, swears at the line mytextView.setText (Html.fromHtml, textWithLinkr.c.hull.fointerException, swears at the line mytextView.setText (Html.fromHtml, textWithLinkr.c.hull.fointerException) null)); - java
  • @java, yes, the textView is a TextView. textToSet is a wrong copy, look at the updated answer. You have an NPE error, which means that you have not initialized TextView . Check that you found it correctly in the markup. - Yuriy SPb
 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Адрес: www.google.ru" android:autoLink="all" android:textSize="14dp" android:id="@+id/texttest" /> 

Drop out all code leave only markup.

And that's all.

  • Just how can I hide the link here so that only the name remains and when I clicked on the name I moved to the entered address? - java
 TextView.setText(Html.fromHtml("<a href=http://www.google.com><font color=#AAA>Your Text</font></a>")); 
  • did not help, is highlighted in red as an error - java
  • @java, learn to debug the code and look for the causes of the "underscore in red as an error", the author forgot the quotes, and you could not see it! Edited the answer. - katso