how to redirect to particular URL while clicking on button in android?
Asked Answered
B

2

13

I want to redirect user to particular URL while clicking on Button in Android App.

Betoken answered 10/2, 2010 at 11:31 Comment(1)
What is your actual question? Did you already try something and then failed? How is this Anroid related?Jobi
C
33

You can start a 'view' activity, which would be the browser given a URL:

public class HelloWorld extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Assuming you are using xml layout
    Button button = (Button)findViewById(R.id.Button01);

    button.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        Intent viewIntent =
          new Intent("android.intent.action.VIEW",
            Uri.parse("http://www.stackoverflow.com/"));
          startActivity(viewIntent);
      }
    });

  }

}
Coreencorel answered 10/2, 2010 at 11:54 Comment(1)
Better to use Intent.ACTION_VIEW instead of "android.intent.action.VIEW"Balakirev
H
0

Update #2023:

If you are using in View-Activity:

startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(URL_HERE)))

If you are using in Compose-View:

@Composable
fun YourComposable() {
    val context = LocalContext.current

    context.apply {
        startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(URL_HERE)))
    }
}
Howie answered 15/7, 2023 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.