2

When I open a Document file (by URL link), i want to show it in WebView, but when I connecting the URL link, my application doesn't show it in WebView, it is opened in chrome. When i disable google chrome, it shown in WebView, but when I enable chrome, the same thing happened again.

Please help me to resolve this issue.

This is my code: MainActivity

btnAccess.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = edtAddress.getText().toString();
            Intent  intent = new Intent(MainActivity.this, Main2Activity.class);
            intent.putExtra("LINK","http://"+ url);
            startActivity(intent);
            /*accessURL();*/
        }
    });

Main2Activity:

webView = (WebView) findViewById(R.id.webView);

    Intent intent = getIntent();
    String url = getIntent().getStringExtra("LINK");

    webView = new WebView(Main2Activity.this);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    setContentView(webView);

    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl("http://docs.google.com/gview?embedded=true&url="+ url);
    setContentView(webView);
Johnett Mathew
  • 287
  • 1
  • 8
  • 18
  • 2
    Possible duplicate of [Android webview launches browser when calling loadurl](http://stackoverflow.com/questions/7746409/android-webview-launches-browser-when-calling-loadurl) – maRShmallow Apr 28 '17 at 03:26

1 Answers1

3

WebView is a system component powered by Chrome that allows Android apps to display web content. So it WILL open in chrome by default.

To open web pages within your application:

main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="match_parent"
android:layout_height="match_parent">
<webview xmlns:android="http://schemas.android.com/apk/res/android">
    android:id="@+id/wvBrowser"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</webview></linearlayout>

mainactivity.java

public class MainActivity extends ActionBarActivity {
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
 
    WebView ourBrow=(WebView) findViewById(R.id.wvBrowser);
    ourBrow.getSettings().setJavaScriptEnabled(true);
    ourBrow.getSettings().setLoadWithOverviewMode(true);
    ourBrow.getSettings().setUseWideViewPort(true);
    ourBrow.setWebViewClient(new ourViewClient());
    ourBrow.loadUrl("http://www.google.com");
   }
}

Create a ourViewClient.java class and copy the below code on ourViewClient.java

package com.example.app;
 
import android.webkit.WebView;
import android.webkit.WebViewClient;
 

public class ourViewClient extends WebViewClient {
 
    @Override
    public boolean shouldOverrideUrlLoading(WebView v, String url)
    {
        v.loadUrl(url);
        return true;
    }
}

Source: https://www.codeproject.com/Questions/740511/how-to-open-webpage-within-android-app-in-startup

arongka
  • 449
  • 3
  • 16