How to make clickable textview
How to make clickable textview
Make part of TextView clickable
It is often necessary to add a link in a block of text and configure its display. Let’s figure out how to do this.
To be able to change the properties of some part of the text you should use implementations of the Spannable interface. These include:
This is a kind of wrapper over String with additional visual properties. TextView can work with Spannable and apply these properties.
Formulation of the problem
Let’s describe the input data of the task. We have a certain text, and we know a phrase from this text, which we want to endow with new properties. Namely — we want to change its color and hang View.OnClickListener on it.
Create the skeleton of our method:
Setup clickable behavior
ClickableSpan requires two methods to be implemented:
Now we need to install this Span in our SpannableString indicating the area where the desired phrase is contained.
Span flag
The Spanned.SPAN_EXCLUSIVE_EXCLUSIVE flag says that formatting will not work when you insert new characters inside the span interval.
For example, there was a line containing a block formatted through StyleSpan(Typeface.BOLD) :
If you insert a new block of text:
we will get 0 15 234. If we aren’t going to change the text, this flag does not play a big role.
Movement method
As a result, we have a method:
High-level logic will look:
That’s all, so we have achieved the desired result. You can optionally pass it to the HashMap method with a set of phrases and an OnClickListener for each.
How to make a textView in android to be clickable or act like hyperlink
I want to a make a textView text like «Meanwhile check the similar products» to be act like clickable link or url.(i.e) when someone clicks on the text it will hit an Api to fetch the data and shows a new activity.
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Try This it may be help to you
To make the TextView automatically identify and make the URLs clickable, you just have to put
property in your TextView’s xml.
Please use like this code
in java file
and you can also use
I’m trying many cases but always fails. But finally found the solution.
If you want TextView with links clickable as below:
You need format your TextView like that:
Note, that android:linksClickable=»true» is important!
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:
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:
I have also tried making the TextView have attribute of android:onClick=»openBrowser» and have this class openBroweser:
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:
(which works here, so I assume it should be a legal link as needed)
My whole XML file as requested in comment
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
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.
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
how to make a word in a complete text view clickable android(not url link)
Although I know there are hundreds of posts in stackoverflow regarding this post still, I did not found my appropriate answer. So I am asking it as new question.
Problem:- I have a text in text view «You need cookies or banana to eat?» I want that if any one click on banana or cookies then only it should go to Kitchen screen. If clicked on other word it should not react.
My Try:- I am doing it from text view, android:clickable=»true» but complete line is getting clickable. So I decided to break it into various text views, and used relative layout but again there was a problem that complete line was not coming in center of screen.When I hard coded it as android:layout_marginLeft=»150dp» but then indentation disturbs for portrait and landscape. and obviously we should not hard code the values. Apart from that to give a clear view to user I need to do some formatting to banana and cookies like bold and underlined and some color. Although I used and tag for bold and underline, how to color the text.?
Lastly I should summarize my problems as
1) How to make some word in a complete string clickable (not url links)
2) How to do formatting of few text of complete string.
3) If relative layout is used how to keep the entire string in center.
If any other approach can b used they are also welcomed
Point to note that these all are not url links but internal screens. So please if any one could help. Any help will be appreciated. Thanks in advance.
How to make a TextView Clickable with a extra ID in android
I am developing a Android Application, where i have multiple textView and I want those textview clickable. When I will click those textView I will get a common empty form but with a static ID in the top for every textview.
For example: If I have 3 product name in 3 text view, if I click on textView1, a previously made layout will be shown and there will be a static ID with my product name in the top, then there will be form to filling up the details of the product.If I click on the textView2 then the form will be same only the Static ID and the product name will change.
I hope you guys understood what I wanted to explain.
I am new in application development so I need some simple solution. So far I have made a layout of my form and I have also made a clickable Layout. So I need to know how I would make the class and function to call the layout and plus the static ID.
Layout of the product name and the Clickable TextView:
ProductList.java file that call the product_list.xml
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
In xml in your textview put : android:onClick=»onClick»
Источники информации:
- http://stackoverflow.com/questions/39094594/how-to-make-a-textview-in-android-to-be-clickable-or-act-like-hyperlink
- http://stackoverflow.com/questions/34582182/android-make-links-in-a-textview-clickable
- http://stackoverflow.com/questions/9495005/how-to-make-a-word-in-a-complete-text-view-clickable-androidnot-url-link
- http://stackoverflow.com/questions/24147708/how-to-make-a-textview-clickable-with-a-extra-id-in-android