Handle android TextView's link click - android

So i have a textView and sometimes my textView contains links, currently when i click in this link an intent with a list of downloaded browsers opens. now what i want is, i wanna open this link inside the current app, say in a WebView. how can i do this?
we do have multiple articles/questions on same topic but i have no idea what this articles/questions are really about.
like this one https://gist.github.com/ksoichiro/3394338 gave me an ClassCastException
android.text.style.URLSpan cannot be cast to pb.myPackage.LinkUtils$SensibleUrlSpan
or this one handle textview link click in my android app
is there's any straight simple way to achieve this? please let me know.

You could use Chrome Custom Tabs, this is faster than a webview in loading webpages and is pretty simple to customize.
First include the Library:
dependencies {
...
compile 'com.android.support:customtabs:25.1.1'
}
Then in your Activity when you call the method to open a browser add these lines of java:
String url = ¨https://google.com/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent intent = builder.build();
intent.launchUrl(this, Uri.parse(url));
Refer to it's docs for more customization at:
https://developer.chrome.com/multidevice/android/customtabs

Related

How to hide url in Chrome custom tab android

I have implemented new Chrome Custom Tab in android, I am using the following code to open
Uri uri = Uri.parse("https://akash.plobal.com/ploicy");
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
intentBuilder.setToolbarColor(Color.BLACK);
intentBuilder.setShowTitle(true);
intentBuilder.build().launchUrl(getActivity(), uri);
I need to hide url below the title in toolbar
If you control both the content and the application, you can use Trusted Web Activities to completely remove the URL bar. You'll need to implement Digital Asset Links to validate the ownership of both.
If you don't own the content, it is not possible to hide the URL below the title. The URLs needs to visible to the user, so that they can know in which site they are. By removing this from the UI, a malicious site could mimic the UI of another one, and this could potentially create a security issue for users.
You cannot hide the url alone. You can only get rid of the whole title bar, that too when the user scroll the view. To achieve this,use
intentBuilder.enableUrlBarHiding();
You can try setShowTitle(false) of your custom tab.

Custom Tabs Support Library

Recently was added this support library, but I couldn't find any example.
What the purpose of this library?
Could you post any example using this library?
CustomTabs is used to open links in a browser that supports CustomTabs. Most likely opening is done on Chrome, hence CustomTabs is part of chromium platform.
Purpose is to avoid implementing WebViews in your application and yet giving you option for styling actual chrome tabs, like toolbar color, title, various exit/enter transition, adding action buttons and menues. CustomTabs will allow your application bind to the chrome service and make chrome work as part of your application. Styling which will give you feel the opened web resource is part of your application.
Beside the styling, CustomTabs will give you full chrome web capabilities that probably couldn't be achieved with standard WebView.
Here are demos, which are straight forward.
Edit:
A snippet from my application which is "simplified" version of the Google's demo, lacking fallback mechanism, for now.
Usage of the helper is the following:
Initialize it when your activity is alive
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview);
mCustomTabHelper = new SimpleCustomChromeTabsHelper(this);
}
2. When the instance is alive and we have an url ready to be opened we can call:
mCustomTabHelper.prepareUrl(mProduct.getRedirectUrl());
Which will bind to the Chrome service, if not previously bind, or will just notify Chrome service that we might be opening that link in the future.
CustomTabSession can be used to open or prepare multiple url.
Open the url
mCustomTabHelper.openUrl(mProduct.getRedirectUrl());
The overloaded method of openUrl is using sort of ui options builder that is replica of the CustomTabIntent.Builder, but I have dropped the CustomTabsSession argument so the helper later will build CustomTabIntent internally.
I'm running Chrome Dev version along stable one. If I choose the stable one, I'm not able to use CustomTabsat all. As Google advices, CustomTabs will only work on Chrome 45 and beta versions of Chrome.
Demo from my application: https://youtu.be/fnIZwuJXjHI
Edit: Post
Try this:
gradle dependency:
dependencies {
...
compile 'com.android.support:customtabs:25.1.0'
}
Code :
Uri uri = Uri.parse("https://github.com/mzelzoghbi");
// create an intent builder
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// Begin customizing
// set toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
// build custom tabs intent
CustomTabsIntent customTabsIntent = intentBuilder.build();
// launch the url
customTabsIntent.launchUrl(activity, uri);
There is demo project on github, mentioned by #NikolaDespotoski, which can be partially reusable.
Solution is based on this article.
Add project shared to your project. Shared is a name of project (I don't know why Google didn't add it into customtabs library). link to shared project
Copy Activity helper from demo project to your project and put correct package. CustomTabActivityHelper
To pre-fetch url use CustomTabActivityHelper#mayLaunchUrl method (if needed) and CustomTabActivityHelper#openCustomTab to open Chrome custom tab.
For instance openning custom tab:
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, uri,
new CustomTabActivityHelper.CustomTabFallback() {
#Override
public void openUri(Activity activity, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Pre-fetching of url is more complicated. You can see this demo for better understanding.

How to get the link coordinates in EBookDroid of Android for opening the links programmatically

I'm developing a EBookreading application, EBookDroid is a library which we are using for the PDF reading. If we have any links in the PDF like www.stackoverflow.com, when the user clicks on it, it supposed to open the link, for that i need to find out the coordinates of that link in the Document.
I am writing like the below.
final RectF linkRect = page.getLinkSourceRect(pageBounds, link);
But it's always giving the null back.
Try this may be use full
THIS EXAMPLE

Android press Button cannot appear Webview program troubleshooting

I implemented the the Android webview encounter some problem
Beginning do a OptionMenu, and he will appear when you press the Menu four locations
I use the mouse click taipei he displayed to Toast
But I would like to implement the functionality Toast message screen webview display the specified web page
I use Intent a = new Intent (Intent.ACTION_VIEW, Uri.parse ();
 startActivity (a);
Can successfully display the webpage
But I would like to the webview implement web pages using setContentView ();
Switch to taipei.xml <= webview, when I click on the submit button, it could not display the webpage using webview
Please help me to see where the problems lie, because beginners just learning, I hope to try to attach the code to solve my problem.
Code URL: [I implemented the the Android webview encounter some problem
Beginning do a OptionMenu, and he will appear when you press the Menu four locations
I use the mouse click taipei he displayed to Toast
But I would like to implement the functionality Toast message screen webview display the specified web page
I use Intent a = new Intent (Intent.ACTION_VIEW, Uri.parse ("http://www.taipei-101.com.tw/"));
 startActivity (a);
Can successfully display the webpage
But I would like to the webview implement web pages using setContentView ();
Switch to taipei.xml <= webview, when I click on the submit button, it could not display the webpage using webview
Please help me to see where the problems lie, because beginners just learning, I hope to try to attach the code to solve my problem.
Code URL: http://www.javaworld.com.tw/jute/post/view?bid=26&id=312960&sty=1#312960
And attach performed on screen picture
And attach performed on screen picture in the cod URL
Implore help thank you

Facebook "feed" dialog Display Issue

I am Trying to post on facebook with the help of "feed" dialog. I supply some information in post like "link","picture","caption","description","title" etc.
But when i share the post the preview look like the image below:
My Concern is this I want to show description right align to the dialog after the Image ends. Can i do this by some change in my code.
The updated Screen looks something Like the Image Below:
mFacebook.dialog(MyActivity.this, "feed",parameters, new FbDialogListener());
Where parameter is the object of Bundle contains all the values to be posted on Facebook.
There is the same issue with Iphone SDK too.
You can't do this, the dialogs are rendered from Facebook's side, as is the content itself once posted. You also appear to have attempted to embed HTML in the description, this will also not work as Facebook will not render client-supplied HTML in their interface.
Do not use Facebook's dialog, you can create your own views (e.g. UIView in iOS or View in Android) similar to the second dialog (or whatever you like). On share button tap you can use Facebook's graph API to share it on your wall.
iOS example
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[appDelegate.facebook accessToken],#"access_token",
<your FB app id>, #"api_key",
<message you want to post>, #"message",
imageData, #"source",
nil];
[appDelegate.facebook requestWithGraphPath:#"me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:appDelegate];
See here for graph API.
I am using a similar approach in my iOS app. I designed the share dialog almost similar to Facebook's share UI.

Categories

Resources