Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a link in browser using Compose for Desktop

How to open a link in the browser if I click on a button. I am using Compose for Desktop for it.

Button(onClick = {
    // What I have to write here..
}) {
    Text(
        text = "Open a link",
        color = Color.Black
    )
}

Thanks in advance.

like image 780
Avijit Karmakar Avatar asked Apr 17 '26 02:04

Avijit Karmakar


1 Answers

Use the Desktop#browse(URI) method. It opens a URI in the user's default browser.

public static boolean openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(uri);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}

public static boolean openWebpage(URL url) {
    try {
        return openWebpage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return false;
}
like image 159
JIm Avatar answered Apr 20 '26 09:04

JIm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!