Skip to content

Instantly share code, notes, and snippets.

@rduplain
Created May 8, 2012 20:08
Show Gist options
  • Save rduplain/2638913 to your computer and use it in GitHub Desktop.
Save rduplain/2638913 to your computer and use it in GitHub Desktop.
A very simple full-screen WebView activity for Android native wrappers, as a starting point.
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);
}
}
@muhammad-adnan
Copy link

Hi I am using your same code, but i dont understand why my jquery mobile not loading in my mobile apps thats really give me tension. Can you help me

@neersoft
Copy link

Try this....

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

@ratbuddy
Copy link

Works great, thanks. One issue: When the device orientation changes, it takes the user back to the very first index.html which is loaded when the app is run. How do I make it remember what page the user is currently viewing, and restore it after an orientation change?

@raelmax
Copy link

raelmax commented May 31, 2014

Nice! To prevent refresh when the device orientation changes, you need add "android:configChanges="orientation|screenSize" to your manifest file on your activity.

@RyLeeHarrison
Copy link

Im getting a "net:ERR_CHCHE_MISS"

@semihbattal
Copy link

Hi, Works great, thanks, but, how I could I modify this program to start Firefox instead of the Android browser? ( In full screen mode )
I am "a dinosaur programmer" but very new to Android programming...
Thanks.

Copy link

ghost commented May 12, 2015

Hey,RyleeHarrison - I think you have not applied the permission in manifes file .
try this ..

@yordiW
Copy link

yordiW commented Jun 2, 2015

The webpage on sitename can't be loaded as: net::ERR_CACHE_MISS.... Anybody? Help me please?

@albertosr86
Copy link

where I put this code?

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

@hungitk46
Copy link

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?

@lvillasica
Copy link

👍 perfect when combined with my Polymer app 😸

@jaltek
Copy link

jaltek commented Oct 29, 2015

@yordiW: You have to set the correct permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

@bulbulhossenbd
Copy link

Good Tutorial

@casenjo
Copy link

casenjo commented Apr 7, 2017

Great starting point, thanks a lot for the gist! :)

@bstanleynerd
Copy link

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.

@robertofabrizi
Copy link

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.

@joshuapinter
Copy link

This was really helpful, thanks! I'd definitely enable Javascript in the main Gist, though:

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

@tirthsuthar
Copy link

Many users, including web or application developers, are confronting the err_cache_miss consistently in Google Chrome browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment