Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see a list from the browsers by clicking on a button

Tags:

android

I have an activity like the following code and I want when I click on the button I see a list from the browsers and when I choose one of them I go to the https://www.google.com website using the chosen browser. I know that I should use the intent method for this, But I do not know that how can I use this method in my manifest file and in my MainActivity.java file.

public class MainActivity extends Activity {

Button button;
String url = "https://www.google.com";
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }}
like image 209
android Avatar asked Dec 06 '25 03:12

android


1 Answers

you can do like this in your button click here url is your url

private static final String HTTPS = "https://";
private static final String HTTP = "http://";

  button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         // To check if url starts with http or https if not then will throw an exception
         if (!url.startsWith(HTTP) && !url.startsWith(HTTPS)) {
           url = HTTP + url;
         }
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
         startActivity(Intent.createChooser(intent, "Choose browser"));

      }
    });


}
like image 150
Satyen Udeshi Avatar answered Dec 12 '25 14:12

Satyen Udeshi



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!