How to add HTML5 files to Android programatically? - android

I developed a game in HTML5. It contains JavaScript and HTML files. Now I want to add them to my Android app. How can I do this?

You need To add Webview to have HTML5 Files to be loaded this can be done like this
public class HelloWebView extends Activity {
WebView webview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new HelloWebViewClient());
webview.loadUrl("file:///android_asset/test.html");
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setDatabasePath(databasePath);
webview.setWebChromeClient(new WebChromeClient() {
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(5 * 1024 * 1024);
}
});
}
And place your HTML5 files inside assets.

Android Developer Website should be the first thing comes to mind. Check WebView Page.
For tutorial you can check :
http://www.mkyong.com/android/android-webview-example/

You can have webview within your android device which load the page that host your html5 game.

Related

How to save data locally in the Android Studio webview

I saw that there are many questions on the subject on the forum, but I do not think that one applies to my case. I'm developing an app through the WebView of Android Studio that lets you shop online. The application must collect the name, phone, and client address at startup. Then, write the data locally to use them as reference to save later on in the database. I used this command to save the data locally:
localStorage.setItem('name', 'John');
But it is not compatible with Android Studio, so I added it to MainActivity:
settings.setDatabaseEnabled(true);
Even so, I could not save the data locally. Is there any way to save and extract the data locally in this case?
The code of MainActivity is:
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file:///android_asset/www/index.html");
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
public class MyActivity extends Activity {
WebView webView = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get rid of the android title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.main_webview);
WebSettings settings = webView.getSettings();
//Enable Javascript
settings.setJavaScriptEnabled(true);
//Enable DOM storage, and tell Android where to save the Database
settings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
settings.setDomStorageEnabled(true);
webView.loadUrl("file:///android_asset/index.html");
webView.setWebChromeClient(new WebChromeClient() {
/** Called when the database exceeds it's maximum size **/
#Override
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
//Double the estimated size of the Database
quotaUpdater.updateQuota(estimatedSize * 2);
}
});
}
}

Loading pdf or url in Android Webview

I am working on an Android application that opens urls its content is HTML or Pdf ... I used this way to open it
WebView webView = (WebView) mContentView.findViewById(R.id.web_view);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
// This is the url that may be .html or .pdf
String url = "https://uva.onlinejudge.org/external/1/119.html";
//String url = "https://uva.onlinejudge.org/external/11/1124.html";
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress >= 100) {
setContentShown(true);
}
}
});
webView.loadUrl(url);
the first url opens normally in the webview, but the second url doesn't show anything in the webview .. and If I try to open it on the browser it redirects me to this url [https://uva.onlinejudge.org/external/11/p1124.pdf] and open normally.
Any help will be appreciated.
Set a download listener to your webview. Inside the listener, you can choose to download the PDF by overriding its methods.
webView.setDownloadListener(new (DownloadListener listener) { // is it a pdf? });

How to stop ads to open in my Webview of Android App?

I am developing a mobile app. On some options I am redirecting the user to different websites like http://m.moneycontrol.com/ , http://www.redbus.com/ etc.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.loadUrl("http://www.m.moneycontrol.com/");
// mainWebView.setScrollBarStyle(MainActivity.this.SCROLLBARS_INSIDE_OVERLAY);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url){
//True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
return true;
}
});
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
When I tested the app on some phones. On 2-3 phones the webview started redirecting to different sites and started opening affiliate ads to install antivirus etc.
Please Suggest me solution to stop these ads.
Change the useragent string of the WebView to that of IE or desktop Firefox. The websites will show the regular version of the site and hopefully the ads won't be there.
webview.getSettings().setUserAgentString(
"Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/12");
The general recommendation is not to open in your app's WebView any sites that you don't control. Instead of doing that, just launch the system browser:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.m.moneycontrol.com/")));

why my webview is opend in a own browser

I have a android code like this:
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://test.com");
the problem is, when I start the app, it is opend in a new browser intend and not in my webView of the app.
how to avoid this?
Add a WebViewClient. This will prevent the default browser to open the URL (or get a selection dialog when there is no default)
myWebView.setWebViewClient(new WebViewClient());
More information can be found on Android website: http://developer.android.com/guide/webapps/webview.html
With the WebViewClient you can do more things, like preventing to load an URL, or change the URL.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// 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;
}
}

How to enable Addressbar in Android WebView?

I´m actually using a WebView in my Application and it works pretty good.
Now I´d like to be able to change the actual URL, just like that Addressbar in the Android Stock browser, where you can see the URL and where you simply can change it.
How can I enable this bar? Or do I have to implement it myself?!
Thanks!
My Code looks like:
private void setupWebView() {
webview = new WebView(this);
webview.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = webview.getSettings();
webSettings.setUserAgentString("foo");
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(false);
webSettings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MyActivity.this.setProgress(progress * 100);
}
});
}
Impossible:
The WebView class is an extension of Android's View class that allows
you to display web pages as a part of your activity layout. It does
not include any features of a fully developed web browser, such as
navigation controls or an address bar. All that WebView does, by
default, is show a web page.
http://developer.android.com/guide/webapps/webview.html
If you want to change URL by code:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

Categories

Resources