The links in the TextView are too tight, often clicking on different links and getting to the same one (the screen is too small). Is it possible to make a separate marker, by clicking on which a list will appear with all the links in the TextView ?
2 answers
I didn’t quite understand what a separate marker is. Here is an example. By clicking on TextView , a menu appears with a list of links.
val textView = view.findViewById<TextView>(R.id.textview) textView.text = "foo bar http://bar.foo bar foo http://foo.bar" //Эта строчка выделяет ссылки Linkify.addLinks(textView, Linkify.ALL) val popupMenu = PopupMenu(context, textView) //Формируется список ссылок textView.urls.withIndex().forEach { (index, span) -> popupMenu.menu.add(0, index, 0, span.url) } //Вешается слушатель на меню, по нажатию на пункт открывает браузер popupMenu.setOnMenuItemClickListener { val url = textView.urls[it.itemId].url val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) startActivity(intent) true } //Нужно для того, чтобы сделать ссылки ненажимаемыми textView.movementMethod = object: LinkMovementMethod(){ override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent) = false } //По нажатию на TextView появляется меню textView.setOnClickListener { popupMenu.show() } - onePS kotlin? I think it would be more appropriate to show this example while on java - iFr0z
- Agree with iFr0z - Lobs
- According to Realm's forecast, in a year Kotlin will overtake Java in popularity among Android developers. - katso
- @katso yes, I myself have already switched to kotlin - it is more convenient :) but this does not mean that everyone switched at once) is the same, if you answer English in English (like popular too) - iFr0z
|
In Java:
textView.text = "foo bar http://bar.foo bar foo http://foo.bar" PopupMenu popupMenu = new PopupMenu(this, view); URLSpan spans[] = textView.getUrls(); //извлекаем ссылки for (URLSpan span : spans) { popupMenu.getMenu().add(span.getURL()); } popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { String url = item.toString(); startActivity (new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } }); popupMenu.show(); } |
\n, spinner or recyclerView - to help - iFr0z