1

WebView is not showing website correctly. Any help would be great! The code ive used work on all another site. Not sure whats the issue. Any thing I should add? Works well in chrome and other browsers so don't know what to do. Any help would be great!

WebView

Chrome

  public class Website extends Activity {
WebView myWebView;
LinearLayout root;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.website);

    myWebView = (WebView) findViewById(R.id.webView);
    myWebView.loadUrl("http://dspart.org");

    root = (LinearLayout) findViewById(R.id.root);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
    myWebView.setWebViewClient(new WebViewClient());
       myWebView.getSettings().setBuiltInZoomControls(true);
       myWebView.getSettings().setSupportZoom(true); 
       myWebView.getSettings().setUseWideViewPort(true);
       myWebView.getSettings().setLoadWithOverviewMode(true);



        getActionBar().setDisplayHomeAsUpEnabled(true);
}


public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("http://dspart.org")) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

    }
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode) {
        case KeyEvent.KEYCODE_BACK:
            if(myWebView.canGoBack()) {
                myWebView.goBack();
            }
            else {
                root.removeView(myWebView);
                myWebView.removeAllViews(); 
                myWebView.destroy();        
                this.finish();
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}



   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            root.removeView(myWebView); 
            myWebView.removeAllViews(); 
            myWebView.destroy();        
            this.finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);

 }
   }}
Community
  • 1
  • 1
user2407147
  • 1,508
  • 2
  • 22
  • 40

2 Answers2

1
YourWebView.getSettings().setJavaScriptEnabled(true);

try enabling javascript on your webview! code snippet above :

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28
  • Hi i have uploaded the code, if you want to have a look. If you aren't sure can you please vote up the post. Thank you for the support. – user2407147 Jul 14 '13 at 11:30
0

Seems like there is an error in the following line if (Uri.parse(url).getHost().equals("http://dspart.org"))

getHost will return dspart.org and not the url with http. Try after modifying it. Thanks

Refer - http://developer.android.com/reference/android/net/Uri.html#getHost()

nvn
  • 52
  • 3