Shubham Agrawal
Shubham Agrawal

Reputation: 9

How to open any URL taken from User in Web Browser in Android

I am trying to open the URL entered by user but clicking on the button the app is not responding.

The Java Snippet of What i have done is :-

 e=(EditText) findViewById(R.id.editText);
 final String s=e.getText().toString();
 browser = (Button) findViewById(R.id.button2);
    browser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(s));
            startActivity(i);
        }
    });

Upvotes: 0

Views: 170

Answers (1)

Ragini
Ragini

Reputation: 332

You initialised s when EditText was empty. So add below line in OnClick() method to get user input-
String s=e.getText().toString();

    browser = (Button) findViewById(R.id.button2);
    browser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s=e.getText().toString();

            Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(s));
            startActivity(i);
    }
});

Upvotes: 2

Related Questions