Android TextView respond to html <a> clicks - android

I have formatted data with commands embedded with tags. I use the HTML class to populate the TextView. Is there a way to intercept the click on the link and get what was clicked?
Html.fromHtml("<a href='CMD:Bahai'>Some Command Link</a>");

I found that the WikiNotes example project from Google does exactly that. It uses the Linkify infrastructure with a Provider and MIMIE type to load the right Activity.

Related

Match a View in WebView from Espresso in Android

I want to match the user name text field inside a WebView which loads the Salesforce login page (but can also be applied to any other page with text fields).
I have tried with:
onView(withHint("User Name")).perform(typeText("test#sf.com"));
But that doesn't work. Any better idea?
This can be accomplished using Espresso Web 2.2 API
onWebView().withElement(findElement(Locator.ID,"username")).perform(webKeys("test#sf.com"));
Please use Locator.Xpath if you don't know ID.
To find xpath first you need to check the html code based on which you can write xpath.
To get html source code you can use chrome inspector.
connect the device to the PC and then open chrome inspector.You can right click on the html code and click on copy xpath to get the xpath.
You can fill form just run js code in WebView.
For example:
webView.loadUrl("
javascript:document.getElementById('username-field').value = 'test#sf.com’;
");

How to access linkify number

I need to make all numbers in a string become links.
The expected action when any of these links is clicked is to append the clicked number to an existing string.
I managed to linkify the numbers by using the following code:
Pattern myMatcher = Pattern.compile("[0-9]*");
Linkify.addLinks(myString, myMatcher, null);
How can I access and retrieve the clicked number in this case?
I tried looking in other questions related to Linkify but seems all are describing ways to have an action that opens an activity or open the default app for that link type (email address/web URL/etc.)
Thanks in advance for you help :)
You can Customize Linkify to append any predefiened string(scheme) into that.
Take a look at the following post Android Developer Blogspot (Search for "Custom Linkify")
For clarity I am describing a portion of that post here:
Linkify will automatically append whatever is matched to a scheme that
is supplied to it, so for the sake of argument let's assume we have a
ContentProvider that matches the following content URI:
content://com.google.android.wikinotes.db.wikinotes/wikinotes/WikiWord
The WikiWord part will be appended by Linkify when it finds a match,
so we just need the part before that as our scheme.
Now that we have these two things, we use Linkify to connect them up:
Pattern wikiWordMatcher = Pattern.compile("\\b[A-Z]+[a-z0-9]+[A-Z][A-Za-z0-9]+\\b");
String wikiViewURL = "content://com.google.android.wikinotes.db.wikinotes/wikinotes/";
Linkify.addLinks(noteView, wikiWordMatcher, wikiViewURL);
Linkify can be used multiple times on the same view to add more links,
so using this after the Default Linkify call means that the existing
active links will be maintained and the new WikiWords will be added.
You could define more Linkify actions and keep applying them to the
same TextView if you wanted to.
Now, if we have a WikiWord in the TextView, let's say MyToDoList,
Linkify will turn it into an active link with the content URI:
content://com.google.android.wikinotes.db.wikinotes/wikinotes/MyToDoList
and if you click on it, Android will fire the default intent for that
content URI.
For this to all work, you will need a ContentProvider that understands
that Content URI, and you will need a default activity capable of
doing something with the resulting data. I plan to cover these in
future blog entries (and soon). In fact, the whole Wiki Note Pad
application is currently undergoing some clean up and review, and will
then hopefully be released as a sample application.

how to automate login to a website in Android using Jsoup?

Very Basic. but i know how to connect to webpage in java the thing which i don't know that how to deal with buttons(Login button) and username(text filed) and password(text field)in android
Just treat it as a form and pass the login credentials using POST method to the page which is mentioned in the action attribute of the login form.
When dealing with buttons, simply get the text from getText() for EditText and then in onClickListener() of the button, fetch the information from jsoup functions by passing the above info as the POST parameters.

How to use html tags in Android TextView to create tweet interface?

I want to create a view like the one explained on (guidelines) https://dev.twitter.com/terms/display-requirements
I want to do something like:
textView.setText("#abc how are you doing #today?");
So, I want to show a basic text, where some words should be clickable to call (if possible a java function and/or) directly open that url in the browser.
How can I do this?
In your string.xml file define a resource,
<string name="tweet">#abc how are you doing #today?</string>
Now use,
textView.setText(getResource().getString(R.string.tweet));
You can do it by using the WebView class
Android SDK WebView Description

AutoLink #mentions in a twitter client

I am building a basic twitter client application. I am trying to figure out how to make the TextView that holds the Tweets to autoLink the #mentions so that they link to the twitter page of whoever it is the same as it does on the twitter website. My guess is that this is going to involve making a custom TextView and adding this into the part that already handles the auto linking of websites,emails,maps and such. Is this right approach to achieving something like this? or should I be using a stock TextView and handling this by parsing the tweet before it gets put into the view? If I should be going the custom view route could anyone point me in the right direction for how to get this capability added to the autolink? And if I should be using the stock TextView and handling it in java before the tweet gets put into the view how do I get it "linkify" the text my only guess is using something like .fromHTML() but I'm not even sure if this supports the tag.
Have a look at the Linkify class, including the interfaces Linkify.MatchFilter and
Linkify.TransformFilter. You should be able to set up a MatchFilter that works on # links, and a TransformFilter that translates them into the appropriate URL format.
Here's a page that walks you through the usage of these classes; it even uses Twitter as an example for using TransformFilter.

Categories

Resources