This is seems like a pretty simple question, but I'm not sure if there's a solution to it. Is it possible to set a custom action for a text link (those blue texts that are underlined) inside a TextView? In other words, is it possible to have a handler do something on an event like onClick on the link?
You need to set the link in the text, and use the autoLink attribute for it. It will use the marked text as link. Otherwise, the only option is to use an OnClickListener as suggested by nicholas.
You can look at the autoLink attribute doc here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:autoLink
If your text is: "You will find it at http://www.example.com", and you set android:autoLink="web", then the link will be highlighted and a browser will start after clicking it.
If you are working with an EditText or a TextView, they both have an setOnClickListener() method which you could override.
If you are working with a WebView, and the content created is yours, you can use JavaScript to handle click's of links.
If you are working with a WebView and the content you are viewing is not yours, I am not sure if there is a way to intercept those, but you should still be able to call setOnClickListener() on a WebView, as setOnClickListener() is defined in the View class. I am just not sure how you would tell whether or not a link was clicked in that WebView...
I encountered this situation too, where I have autolink set up in TextViews to automatically handle URLs in TextViews. However, the default behavior is to open the web page in an external browser, so how do we make it open in an in-app browser (for example)?
Underneath the hood, this is handled for TextView by LinkMovementMethod. Hence, the suggestion (by one of the answers) to customize onClickListener() of the TextView is not ideal, as it would involve reimplementing much of what LinkMovementMethod handles, so that it could precisely handle clicks/taps on what are identified as URLs.
More recently (years after this post was made), someone has come up with an enhancement over LinkMovementMethod, known as (what else?) BetterLinkMovementMethod, described in more detail this blog post. I can confirm that it did work for me. With just a few lines of code changes, the TextView autolinks now open in the in-app browser (and clearly, all kinds of other behaviors can be customized as desired). I have no affiliation with BetterLinkMovementMethod or its creators, but am just a grateful user.
Related
I am implementing a text editor (not using TextView or EditText components) and when a word is misspelled I would like to bring up a popup similar to what TextViews have after you tap on the word. Is this UI the TextView brings up available in the android framework to use or will I have to create my own custom UI for this?
I can't seem to find anything after searching through the documentation. All I can find is a spell checker framework which just tells you if a word is misspelled and gives you an array of suggestions. Is there something in the input method service that might provide this UI?
Edit: I also need the "Add To Dictionary" option in the context menu so the user can add new words to the user dictionary. Similar to how TextView does it.
I haven't implemented it myself yet, but it looks like using a ListPopupWindow will work. It also interacts with IME if you wish and anchors itself to a parent view.
I have a webview that is displaying a page that looks like a calendar and on that calendar are events that are buttons which can be single or double-clicked - single/double calling a different function. Note: I cannot change the coding of this page. Here is a snippet of a button: <div class="boardpiece clickable" onclick="selectPairing(event, this);" id="sn_18067063" The ID for the button is dynamic - or will always be unknown - so I cannot use it in my coding. The classes of the items I need my clicks to work on, however, are known.
Now, there are only a few classes of buttons here so I can designate what to do for each class if that is a solution. I think I saw a question/answer saying that you may be able to identify the element by an HTML attribute? Something like "If outerHTML contains "boardpiece clickable" Do function "selectPairing" ..." That's my train of thought at least.
Or am I going about this the wrong way? I simply need to call the onclick functions of these elements if the user clicks (taps) on the element. If I were creating the buttons myself, that would be simple. I think I can set a stop function in my onClick so that only the one button is activated since they all have the same class.
~ Dan
If you want to perform any operation on a HTML page inside a web view then you have to modify the code. you need to set onClick event of a button using JavaScript or you have to get element from the HTML page based on the ID.
I am working on a notepad application for android and it seems I have a litte problem for a textview. Here's the thing: I have a listview of notes, when I click a note, I open another activity which displays the content of my note into a textview. When I click this textview(representing the text from the note), it opens another activity for editing the text (like most of these apps do). The problem is that some of the notes are really long, so I need to make my textview Scrollable. But since I am having a "setOnclickListener" on it, I cannot do that at the same time. I have searched a lot on google and apparently a lot of folks seem to have this problem. Is there a way to achieve my goal?
Ideas:
Use OnGestureListener or OnTouchListener
Use an 'edit' button
Use long press instead of OnClick
After a lot of searching for documentation on how this can be done I am finally posting this on SO.
Currently in my app I have implemented a search feature to search text in webview. I am using the inbuilt webview methods to search and highlight all text entries, of the text searched by the user, in the webview.
I now want to change the default highlight color to my own custom color and not use the default implementation. I have been trying to look over everywhere for this but I did not see any specific way of how this can be done.
Can someone please give some hints/suggestions on how this can be done. Very thankful if you stopby and put some of your thoughts.
Does this do the trick for you:
http://danbarnett.net/how-to-hide-android-webview-highlight-border-or-change-its-color-142.html
From the sounds of it you can't actually change the default but you can work around it, or over-ride it using jquery and some css..
And some more Google results of interest:
http://www.icab.de/blog/2010/01/12/search-and-highlight-text-in-uiwebview/
And an overflow question with good answer:
Android webview: highlight a specific word in a page using javascript?
To get a TextView to display (and act friendly with) Html strings my code looks something like:
// itemHtml is a String of HTML defined above
TextView itemContent = (TextView) findViewById(R.id.itemContent);
itemContent.setText(Html.fromHtml(itemHtml));
itemContent.setMovementMethod(LinkMovementMethod.getInstance());
If the Html string has a link, TextView results in links that are clickable and focusable. When the user focuses on a specific link (e.g. by using the d-pad), the link text changes in some significant way to show that focus was obtained.
The problem is that when I test this same pattern using devices with a d-pad using Honeycomb (e.g. a Google TV) or Ice Cream Sandwich flavors of Android, the link in the text shows no noticeable indication that the link has focus.
I know it is getting focus, because when you then hit enter it does the specified action. You can even move around between various links in the text; you're just left guessing which link you're currently at, which results in a very bad user experience.
Is there something I'm doing wrong? Is there some way to fix this or work around this?
Edit: After going a bit nuts, I finally thought I found a solution. However, this solution only works for Honeycomb. ICS is still not solved!
As of API 11, Android has a new setting on TextViews for defining whether the text is selectable.
You can set it using setTextIsSelectable(true) on a TextView, or define it in the XML layout android:textIsSelectable="true"
It's probably best to define it in XML, so that keeping the app backwards-compatible is trivial. Just make sure you're targeting version >= 11, or you'll probably get an error.
The way HTML.fromHTML operates is by creating "spans" with varying effects throughout the various characters of the string. One workaround for this would be to use ClickableSpan coupled with another of the CharacterStyles to colorize the text as clickable. The previous span will allow you to register a callback, and this callback could be to broadcast an intent to view a url (which would open a browser).
The text colour state lists for Honeycomb+ might not set the focused state to a different colour, or you override the colour to be constant.
Check the colors + styles in your_android_sdk_directory/android-14/data/res/
Setting the text to android:autoLink="web" might also help?
The best way to do that is to add CSS styling to your html. I know Android supports :hover selector. So you might right something like this:
String myLink = "your link"
Html.fromHtml(myLink);
and find a way to include CSS data to it: (I'm not sure how but I think it's possible)
a :hover {
color: red;
}
UPDATE:
I think the answer of your question is there.