deepthi
deepthi

Reputation: 8487

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

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

Upvotes: 13

Views: 39620

Answers (2)

Mohammad Reza
Mohammad Reza

Reputation: 95

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

Upvotes: 0

mtmk
mtmk

Reputation: 6316

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

  }

}

Upvotes: 33

Related Questions