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));
Related
I want to open link like "intent://qr.nspk.ru/url-something/#Intent;scheme=someScheme;package=com.example.android;end#Intent;scheme=someScheme;end" from an android application.
If I write something like this:
val i = Intent(Intent.ACTION_VIEW, Uri.parse(androidUrl))
startActivity(i)
I get this error:
No Activity found to handle Intent { act=android.intent.action.VIEW
dat=intent://qr.nspk.ru/url-something/#Intent;scheme=someScheme;package=com.example.android;end#Intent;scheme=someScheme;end
But If I go to this link from browser, it opens and it open an app that I need. Can you help me to understand why I can't open link from app and how to fix it?
The intent: scheme is used for the browser to encode an Android Intent. If you want to use this URL from an Android app, you need to create an Intent from the URL like this:
val i = Intent.parseUri(androidUrl, Intent.URI_INTENT_SCHEME)
startActivity(i)
Since Android 12, links can only be opened with one "approved" app.
As of right now I have my app's supported urls defined in AndroidManifest.xml as intent-filter.
Also I have a button in my app that creates an intent to open a link in browser.
With Android 12 limitations though, if I have the url tied to my app, clicking the open in browser button does re-open my app, which is quite an unwanted behavior.
Is there a way to force a url open in chrome (or other browser)?
I checked the android developer documentation but have not found anything about it.
Thank you
You can specify the default browser explicitly as the package to handle your intent:
val defaultBrowserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
// This would be Chrome if it's the selected default browser
val defaultBrowserPackageName = packageManager.resolveActivity(
defaultBrowserIntent,
PackageManager.MATCH_DEFAULT_ONLY
)
?.activityInfo
?.packageName
val yourIntent = Intent(context, ...).apply {
package = defaultBrowserPackageName
}
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 have a watch face that I've created and I was looking into adding an "About" screen in the watch settings (on the watch). This screen would show the current version and show a button to open a URL leading to a changelog. I want the user to be able to click the button on their watch, and have it open the specified URL on the phone.
Is this possible without me creating a mobile companion application?
You can do this if the watch is running Android Wear 2 using a RemoteIntent, as such:
Intent intent = new Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(Uri.parse("http://www.google.com"));
RemoteIntent.startRemoteActivity(context, intent, null);
...replacing http://www.google.com with your desired URL, of course.
AFAIK, there's no way to accomplish this on Wear 1.x without sending a message to your handheld app and programming it to open the URL itself.
Note the new way to do this is with RemoteActivityHelper:
val remoteActivityHelper = RemoteActivityHelper(context, Executors.newSingleThreadExecutor())
val result = remoteActivityHelper.startRemoteActivity(
Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(
Uri.parse("http://www.google.com/")
),
null
)
and if you want to show the nice "Open on phone" animation, use ConfirmationActivity:
startActivity(
Intent(this, ConfirmationActivity::class.java)
.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.OPEN_ON_PHONE_ANIMATION))
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 :)