23

I want to redirect the user to another activity that opens a internet url. On button click.

Here is my code so far

Button(onClick = { val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")) // Here is some code that starts the activity }) { Text(text="APPLY HACK") } 

3 Answers 3

30

You can use something like:

val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")) val context = LocalContext.current Button(onClick = { startActivity(context, intent, null) } ) { Text("BUTTON") } 
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is not ful. It workds only when you are in activity. Not in separate composable function
6
val context = LocalContext.current Button(onClick = { context.startActivity(Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"))) } ) { Text("Click here") } 

Comments

0
val context = LocalContext.current Button(onClick = { val webIntent: Intent = Uri.parse("https://stackoverflow.com/users/18910957/oolyvi?tab=profile").let { webPage -> Intent(Intent.ACTION_VIEW, webPage) } try { startActivity(context, webIntent, null) } catch (e: ActivityNotFoundException) { // Define what your app should do if no activity can handle the intent. } ) { Text("APPLY HACK") } 

You can do it something like this, just parse link address to the Uri and then call the intent for an action

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.