In Kotlin how do I open a link in new Window

12.7k views Asked by At

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!!!

2

There are 2 answers

2
Mohit Suthar On

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)
}
0
Vikash Bijarniya On

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));