-
-
Save rduplain/2638913 to your computer and use it in GitHub Desktop.
package com.willowtreeapps.demo; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.KeyEvent; | |
import android.view.Window; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
public class MainActivity extends Activity { | |
private WebView mWebView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
getWindow().requestFeature(Window.FEATURE_NO_TITLE); | |
mWebView = new WebView(this); | |
mWebView.loadUrl("http://m.willowtreeapps.com/"); | |
mWebView.setWebViewClient(new WebViewClient() { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
view.loadUrl(url); | |
return true; | |
} | |
}); | |
this.setContentView(mWebView); | |
} | |
@Override | |
public boolean onKeyDown(final int keyCode, final KeyEvent event) { | |
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { | |
mWebView.goBack(); | |
return true; | |
} | |
return super.onKeyDown(keyCode, event); | |
} | |
} |
👍 perfect when combined with my Polymer app 😸
@yordiW: You have to set the correct permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Great starting point, thanks a lot for the gist! :)
This is just a starting point but it is worth noting that WebViewClient.boolean shouldOverrideUrlLoading (WebView, String) was deprecated at API level 24. The documentation for the alternative method, shouldOverrideUrlLoading (WebView, WebResourceRequest) says this:
This method is also called for subframes with non-http schemes, thus it is strongly disadvised to unconditionally call loadUrl(String) with the request's url from inside the method and then return true, as this will make WebView to attempt loading a non-http url, and thus fail.
Is this similar to using Chrome in terms of performance? I followed another tutorial and it works, but it's extremely slow compared to using Chrome directly.
This was really helpful, thanks! I'd definitely enable Javascript in the main Gist, though:
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
Many users, including web or application developers, are confronting the err_cache_miss consistently in Google Chrome browser.
I don't set full screen in WebView after use code getWindow().requestFeature(Window.FEATURE_NO_TITLE);
I don't know code above for what?