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);
}
});
}
}
Related
I am trying to load http://www.bing.com or https://www.bing.com in web view but it is not loading. When I debug I found that the onPageFinished() callback is executed and the web view is still blank.
I have tried with other websites such as http://www.google.com it works fine.
Here is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
WebView webView = findViewById(R.id.web_view);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewController());
webView.loadUrl("http://www.bing.com");
}
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In the logs I'm getting the following message when I'm trying to load bing website
2020-04-10 17:41:31.981 6044-6044/com.mawinbet.android I/chromium: [INFO:CONSOLE(0)] "The resource http://www.bing.com/rb/2U/cj,nj/6BsKE-ZQeKe0vHb0vclH0k01aag.js?bu=BUBKDlVY was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.", source: (0)
Can you please tell me how to solve this problem.
I built an android app that gives two choices, to open a special webpage inside a web view or through browser. The webpage containing java script and ajax, return a list of items.
There is no problem when the webpage is opened through browser but when trying to open the webpage through a webview, it return list of items but when I click on one of them it return an error message
an internal error has occurred
(TypeError) __gwt$exception:<skipped>:cannot call method 'setItem' of null
Here is my code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web = (WebView)findViewById(R.id.webView);
Button browser = (Button)findViewById(R.id.Browser);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebChromeClient(new WebChromeClient());
web.loadUrl("http://10.45.18.22:8080/feeds-portlet/List.jsp?a=xy#gmail.com");
final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://10.45.18.22:8080/feeds-portlet/List.jsp?a=xy#gmail.com"));
browser.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(i);
}
});
}
I have already enabled javascript in the webview !
I am developing an android application. It is a simple application containing a webview which will load my mvc website.
First page of my application is login page.Once user logs in to the system application page is shown.
Requirement - Is it possible for webview to remember the credentials and allow the auto log in to system when he/she visits next time?
need help. Following is main activity code.
public class MainActivity extends Activity {
WebView webView;
String loginUrl = "mail.xyzabcd.com";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Adds Progress bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setLightTouchEnabled(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
webView.loadUrl(loginUrl);
// on clicking a link load it in the same link
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
}
Web View is just a widget which is used to display. It cannot by itself remember data across sessions (if the activity is killed).
What you could do though is to create a resource file that would store the user id and password (based in the logic you want) and then have the Web View check to see if this resource file is present. If present, you could extract details from there. You could do your own encryption as well to secure the data.
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.
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");