LazyLoading
LazyLoading

Reputation: 25

Webview keeps reloading same page

How do i prevent my below code for reloading the same page when a link has been clicked inside the website?

protected void fetch_web(){
    WebView web;
    web = (WebView) findViewById(R.id.voice_mail_view_port);
    NoNetwork = (Button) findViewById(R.id.btn_no_internet);
    NoNetwork.setVisibility(View.GONE);
    String mdb = "http://192.168.23.1/default.php?appid=1.0";
    getWindow().setFeatureInt(  Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    final Activity MyActivity = this;
    web.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {

            MyActivity.setTitle("Fetching feeds...");
            MyActivity.setProgress(progress * 100);

            loader();

            if(progress == 100) {
                MyActivity.setTitle(R.string.app_name);
                deLoader();;
            }
        }
    });
    web.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String
                failingUrl) {
            deLoader();
            alert("No Internet Connection","Let's get connected to see feeds");
            //web.setVisibility(View.GONE);
            NoNetwork.setVisibility(View.VISIBLE);
        }
    });
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl((mdb));
}
protected  void loader(){
    loading = (ProgressBar) findViewById(R.id.loader);
    loading.setVisibility(View.VISIBLE);
}
protected  void  deLoader(){
    loading = (ProgressBar) findViewById(R.id.loader);
    loading.setVisibility(View.INVISIBLE);
}
protected  void onStart(){
    fetch_web();
    super.onStart();
}

This is started when i added custom error message code

    web.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String
                failingUrl) {
            deLoader();
            alert("No Internet Connection","Let's get connected to see feeds");
            //web.setVisibility(View.GONE);
            NoNetwork.setVisibility(View.VISIBLE);
        }
    });

My expectation is to allow the app open the link in other browsers installed in the phone or open it in the same app.

Note: All links displayed on the webview are download link

Any help you see to change or add, am just new to Java thus my first app

Upvotes: 1

Views: 1530

Answers (3)

Ram Koti
Ram Koti

Reputation: 2211

try the above code to open url in your own application: xml file:

<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

java file:

private WebView webView;
webView = (WebView) findViewById(R.id.web_view);`
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://google.com");

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this code: inside your new WebViewClient()

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("www.example.com")) {
        // This is your web site, so do not override; let WebView load the page
        return false;
    }
    // Otherwise, the link is not for a page on your site, so launch another Activity that handles URLs
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
    return true;
}

shouldOverrideUrlLoading reference

Upvotes: 1

Pratik Gondil
Pratik Gondil

Reputation: 699

If want open a link in other browser.try this.

    Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(your_web_url));
startActivity(intent);

hope this helps.:)

Upvotes: 0

Related Questions