I'm a web developer. I'm currently developing android application on Android Studio using WebView which access my website as an android application. One of my webpage contains many external links. My goal is to make the android application can handle external links like Gmail App does (also like facebook and Line do).
Below is the example of gmail app.
An email contains external link
Link clicked, then application open a new activity acts like a browser without leaving Gmail application
Any idea how to make it?
It is pretty simple. You have to use Chrome Custom Tabs as suggested by Gergely as well in comment. Below is the small functional code that will help you to achieve this.
First add this dependency to your build.gradle(Module:app)
compile 'com.android.support:customtabs:23.4.0'
Second add below function to your code and simply pass string URL to it.
private void redirectUsingCustomTab(String url)
{
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// set desired toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
// add start and exit animations if you want(optional)
/*intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);*/
CustomTabsIntent customTabsIntent = intentBuilder.build();
customTabsIntent.launchUrl(activity, uri);
}
Rest it will take care itself. Since Chrome Custom Tabs can customised so lot can be done like you can add menu to toolbar. For detailed information you can visit official documentation from Google itself here.
Hope it will help you to start with :)
Related
for some time, I have been observing that modern android apps(Telegram) use a new way to open urls in the app. By default, the apps I have made, use ACTION_VIEW and the external browser open the url. These apps now manage that intent with a kind of in-app browser witch adapts to the same style. Any idea of what its?
sample
Those are Custom chrome tabs.In app browser.instead of using webview to open your Url, you are going to customize your chrome browser,to look alike your app.you can modify the toolbar tab color as per your app theme .You could give enter and exit animation like fragments transition.So the user wouldn't feel a big transition from your app to browser.For implementation details check this link https://developer.chrome.com/multidevice/android/customtabs
For security reasons also it could be useful,as because you are not going handle those url on you own or inside your app(there might be some security issues with JavaScript)
This is done with WebView. You will find the documentation very interesting https://developer.android.com/reference/android/webkit/WebView
This is achieved by WebView in an Activity that displays web pages. Instead of
Uri uri = Uri.parse("https://www.google.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
You will load the page by calling:
webviewObj.loadUrl("https://www.google.com/");
You can check the official document which describes the whole process. Basic Usage of WebView has been described in this blog which is easy to comprehend and implement.
do the following:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
try this it helps you.
How can I rewrite the code to open link on Chrome custom tab:
imageView5 = (AppCompatImageView) findViewById(R.id.image_view5);
imageView5.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view3) {
Intent intent1 =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://mekotube.com/muslim/"));
((HOMEActivity) getContext()).startActivity(intent1);
}
});
The documentation for Custom Tabs is available here.
On the Custom Tabs Github repo, There is a set of demos, showing how to use the API.
Check out the simple demo app, showing how to open it.
I'd also recommend checking out the demo on how to use the Custom Tabs Service to improve speed when opening the page.
Make sure to check out the best practices as well, as they contain valuable hints on how to handle multiple browsers among other stuff.
Here's an extract from the docs, showing how to open a simple tab, without any customisation or connecting to the Custom Tabs service.
1 - Add the Custom Tabs Support Library to your Gradle build file.
dependencies {
...
implementation 'com.android.support:customtabs:28.0.0'
}
2 - Use the Support Library to open a link:
String url = "https://paul.kinlan.me/";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
I'm using chrome CustomTab to open a url inside my app, using customTabsIntent.launchUrl method and CustomTabsIntent.Builder. Here's how I'm doing it right now:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(someUrl);
is there any way to disable the swipetorefresh behaviour that is set by default, in the CustomTab? by modifying the builder or something? (without making any changes to the website)
No, This is not currently possible, as Custom Tabs will respect the behaviour of the opened web page. If changing the page is possible, these instructions can help
I am a tyro in Kotlin but I have a good Knowledge of Android and Core java. I am stuck on one condition while developing an android app via the Kotlin assistance.
I want that when user clicks a link present on the pdf document; the link should be opened on a browser (and if browser is opened then the link should open on the new window not new tab of the same window ).
I have achieved much of my objective but I didn't found out how to open a link in the new Window if the browser is open already?
I have tried the code below(when the link on the pdf is clicked then it redirects to the below function call):
fun web_page_open(urls: String) { // for more than one url
val uris = Uri.parse(urls)
val intents = Intent(Intent.ACTION_VIEW, uris)
startActivity(intents)
}
I have tried my level-best to explain my problem and also searched a lot(on github as well) but all my efforts went in vein.
Any help is warmly welcomed.
EDIT: Let's consider an instance if the user has already opened the default browser (say ABZfox) then when the link inside the pdf(or a doc) is clicked then the new Window of ABZfox opens instead of the same window in which the user was previously working. I'm sure the question makes some sense now!!!
You can try this one, might be helpful, open new tab of web browser like
fun openNewTabWindow(urls: String, context : Context) {
val uris = Uri.parse(urls)
val intents = Intent(Intent.ACTION_VIEW, uris)
val b = Bundle()
b.putBoolean("new_window", true)
intents.putExtras(b)
context.startActivity(intents)
}
You may use chrome custom tabs instead
To use it you need to add below dependency to your gradle
compile 'com.android.support:customtabs:23.1.1'
Now use below code to open the url
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.enableUrlBarHiding();
builder.setShowTitle(true);
builder.setToolbarColor(Color.TRANSPARENT);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getActivity(), Uri.parse(url));
I am using the below code. It opens the Facebook Messenger app, but the image is not shown (with broken image symbol) and sending fails.
List<Bitmap> abc = new ArrayList<Bitmap>();
abc.add(pic);
FacebookDialog.PhotoMessageDialogBuilder builder = new FacebookDialog.PhotoMessageDialogBuilder(LocationShare.this);
builder.addPhotos(abc);
builder.build().present();
is there any problem in the way i add photos? i mean using List
i have mentioned content provider in android manifest.xml
i am able send link using below code.
FacebookDialog.MessageDialogBuilder builder = new FacebookDialog.MessageDialogBuilder(getActivity())
.setLink("https://developers.facebook.com/docs/android/share/")
.setName("Message Dialog Tutorial")
.setCaption("Build great social apps that engage your friends.")
.setPicture("http://i.imgur.com/g3Qc1HN.png")
.setDescription("Allow your users to message links from your app using the Android SDK.");
Did you add a content provider?
If you look at the javadocs for addPhotos (https://developers.facebook.com/docs/reference/android/current/class/FacebookDialog.PhotoMessageDialogBuilder/#addPhotos) it says that a NativeAppCallContentProvider must be specified in your AndroidManifest.
In the docs for NativeAppCallContentProvider (https://developers.facebook.com/docs/reference/android/current/class/NativeAppCallContentProvider/) it tells you how to add the content provider.
You can also have a look in the HelloFacebook sample which has an example of adding the content provider in the AndroidManifest.