3

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

            }
        });
    }}
android
  • 33
  • 2
  • Possible duplicate of [How To Open Web Page Within My App?](http://stackoverflow.com/questions/32984955/how-to-open-web-page-within-my-app) – MurugananthamS Oct 09 '15 at 11:00

2 Answers2

3

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

      }
    });


}
Satyen Udeshi
  • 3,223
  • 1
  • 19
  • 26
0
   Hope this wil help u
  In your web view layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#c7bbac" >

   <ImageView
    android:id="@+id/txtmain"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/topbar50" />
<ImageView
    android:id="@+id/backbutn"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:adjustViewBounds="true"
    android:paddingTop="2dp"
    android:src="@drawable/backbtn" />
  </RelativeLayout>

 <WebView  

 android:id="@+id/webView1"
 android:layout_below="@+id/relay"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 />
 </RelativeLayout>

  Webview Button Onclick:
   webbutton=(ImageView)findViewById(R.id.web);
    webbutton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(),       WebViewActivity.class);
            startActivity(intent);
          }

        });

   Webview Activity:

    public class WebViewActivity extends Activity {

   private WebView webViewurl;
   ImageView back;


  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    back=(ImageView)findViewById(R.id.backbutn);
    webViewurl = (WebView) findViewById(R.id.webView1);
    webViewurl.getSettings().setJavaScriptEnabled(true);
    webViewurl.getSettings().setBuiltInZoomControls(true);
    final Activity activity = this;
    webViewurl.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String   description, String failingUrl) {
               Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });
    webViewurl.loadUrl("http://Your url");
    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
         }
      });

     }
   } 
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49