Adding menu options to webview in android - android

I have scoured the net for this solution but am thus far unsuccessful. Hopefully someone can help me. I am new and don't know java programming. From tutorials over the web, i have created a "webview" app that loads local html files. This works fine. What i'm trying to do is add the options menu by clicking the "menu" button on the phone so i can quit(exit) the app. There are many tutorials out there for this but when i try to add the code my java file, my existing code in the file start coming up with errors & then everything goes haywire. I am hoping someone can add the appropriate code to my existing code below or correct my code if it is wrong with the menu options included to quit the app. Thank you in advance.
Su
package com.xrefguide;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class XRefGuide extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView web = (WebView) findViewById(R.id.webView);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file:///android_asset/index.html");
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
web.getSettings().setPluginsEnabled(true);
web.getSettings().setSupportMultipleWindows(false);
web.getSettings().setSupportZoom(true);
web.setVerticalScrollBarEnabled(false);
web.setHorizontalScrollBarEnabled(false);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
//Our application's main page will be loaded
//web.loadUrl("http://mapa.org.my");
web.setWebViewClient(new WebViewClient() {
#Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
}
}

There is nothing in your code that has anything whatsoever to do with an options menu. Here are two sample projects demonstrating the use of options menus and context menus, one using menu XML files, and one defining the menus purely in Java.

Related

Ionic 3 application - how to make font size independent of native settings?

I am developing an Ionic 3 application, I recently published it and in production, where phones of all kinds and sizes were used, I noticed that there is an android native setting called "font-size" where you can make the size of texts in your phone bigger or smaller.
Some people (among others many older people) choose 'big' or even 'huge' text size. This unfortunately affects texts in my application on their phones and completely ruins the layout.
The way I am defining font sizes in my css files is with em values, but I also tried px.
Do you know if there is any way to prevent my application from adhering to this native android text-size setting? Or any other ways to fix it?
Please help,
Cheers
I found the solution! I used this phonegap plugin:
https://ionicframework.com/docs/v3/native/mobile-accessibility/
and used the method this.mobileAccessibility.usePreferredTextZoom(false);
This way, my app ignores the android font size settings!
I face similar issue. I have done as below for best practice :
this.platform.ready().then(()=>{
this.mobileAccessibility.getTextZoom().then((textZoom)=>{
if(textZoom>130){
this.mobileAccessibility.setTextZoom(130);
}
});
})
Setting text zoom with JS works with a jump from big to small size. But in MainActivity.java it applies immediately without visible delay
package io.ionic.starter;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.getcapacitor.BridgeActivity;
public class MainActivity extends BridgeActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setTextZoom(100);
}
}
The plugin suggested in the accepted answer was last updated in 2016. Instead of adding a new plugin, we can configure the webview settings to disable text zoom as discussed here.
So, inside MainActivity.java you can do something like this:
import com.getcapacitor.BridgeActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends BridgeActivity {
public void onResume() {
super.onResume();
WebSettings settings = bridge.getWebView().getSettings();
settings.setTextZoom(100);
settings.setSupportZoom(false);
}
}

"The file must have been renamed, moved, or deleted" warning in Android webview

I have a very basic Android app in Google's Play store. It's basically one window with a webview showing local html content. Most users who download the app have no problems with it. However, a few are not able to use it at all. When they try to open it, they get a warning "The file must have been renamed, moved, or deleted". In other words their phone or tablet can't locate the html files that are packaged together with the app. I can't replicate this problem and have no idea where it's coming from. I wonder if anyone here run into this issue before and resolved it.
For reference, the app design is shown below (nothing special). A bunch of "htm" files is located in "assets". One of them is referenced in the code explicitly - "Contents.htm". There's no css or javascript, but it didn't seem to make any difference as far as this particular error is concerned. This problem is experienced by people outside the US (so far), which may be another clue as to what it could be.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</WebView>
MainActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///android_asset/Contents.htm");
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
One of my users finally traced the problem to the app called WPS office. Indeed, when WPS office is installed on a device, my app stops working. Apparently WPS hijacks internal links within android webviews and tries to open them by itself. There are no obvious settings to change that behavior, but after uninstall everything returns to normal. I hope this bit of information may be helpful to someone someday.
Try this.
WebView browser = (WebView) findViewById(webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebChromeClient(new WebChromeClient());
browser.loadUrl("file:///android_asset/www/index.html");
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
I found the solution:
WPS office hijacks all links in any webview. You can build a workaround like this:
Write a Javascriptinterface and every link should call a Android-Function which load the specific Webview-Url
#JavascriptInterface
public void startGame() {
mWebView.post(new Runnable() {
#Override
public void run() {
mWebView.loadUrl("file:///android_asset/www/game.html");
}
});
}
With this workaround everything works fine: You can test the workaround in my app
https://play.google.com/store/apps/details?id=de.devbert.circlecommander

Android cordova chromeview : Not implemented reached in virtual quota::SpecialStoragePolicy

I'm working on a phonegap project which contains a canvas overlayed with kinetic.js, which allows a user to pinch zoom and pan around an image, then draw annotations on it. it works spliendidly in a browser and on windows and apple tablets, but of course android is a good bit slower.
as a solution, i've released the app using https://github.com/thedracle/cordova-android-chromeview. after switching my main java class to use ChromeView as the webview, i'm getting this error on startup:
12-03 13:21:09.083: E/chromium(13917): [ERROR:aw_browser_context.cc(191)] Not implemented reached in virtual quota::SpecialStoragePolicy* android_webview::AwBrowserContext::GetSpecialStoragePolicy()
after debugging through the codebase, it looks like the error is triggering here:
private void setNativeContentsClientBridge(int nativeContentsClientBridge) {
mNativeContentsClientBridge = nativeContentsClientBridge;
}
(AwContentsClientBridge.java line 36).
i'm trying to find out what the nativeContentsClientBridge int is. My value is 1611312352 but i haven't a notion of what that represents.
my gut feel is that the chromium browser is missing an implementation for accessing localstorage. i found this bug:
https://github.com/pwnall/chromeview/issues/27
where someone is experiencing the same thing, but there is no solution.
for assistance, this is my main activity class:
package com.companion;
import org.apache.cordova.Config;
import org.apache.cordova.CordovaActivity;
import us.costan.chrome.ChromeSettings;
import us.costan.chrome.ChromeView;
import android.os.Bundle;
public class CompanionApp extends CordovaActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
ChromeView chromeView = new ChromeView(CompanionApp.this);
ChromeSettings settings = chromeView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
setContentView(chromeView);
super.loadUrl(Config.getStartUrl());
}
}
Thanks for your help,
Margaret
Don't know if it's normal, but it seems that a method's not implemented in Chromium base code : https://github.com/01org/pa-chromium/blob/master/android_webview/browser/aw_browser_context.cc
Here's that particular method:
quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
// TODO(boliu): Implement this so we are not relying on default behavior.
NOTIMPLEMENTED();
return NULL;
}
I hope it helps :)

How to enable cookies for Android phonegap 1.8.0 app?

I understood that in order to use cookies inside of phonegap native app there must be piece of code which enables it.
When building phonegap for iOS using xcode 4 there is such piece of code inside of phonegap template.
Could you please advice me which code and where I need to put in order to enable cookies for Android phonegap 1.8.0 app?
Please note that I'm using the eclipse Indigo 3.7.2 for building of the app.
Many thanks.
Cheers,
Sinisa.
If you are trying to use local cookies (file://) you have to make the parent Phonegap project accept local cookies. To do so, You should have a file called youappname.java in your PhoneGap project, probably with this contents or similar:
import android.os.Bundle;
import org.apache.cordova.*;
public class App extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
Modify it to look like this example:
import android.os.Bundle;
import android.webkit.CookieManager;
import org.apache.cordova.*;
public class App extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
CookieManager.setAcceptFileSchemeCookies(true);
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
you actually wanna do
import android.webkit.CookieManager;
import org.apache.cordova.*;
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
try{
CookieManager.setAcceptFileSchemeCookies(true);
}catch(Throwable e){}
Since the CookieManager does not exist on older Android versions
If my memories are good, the Phonegape's template loads your start-up web page (ex: index.html) by loading it from the webview (something like : super.loadUrl("file:///android_asset/www/index.html");). The latter is declared in the main activity (a ".java" file).
So, first, find the instruction that loads your web application. Then, add the following lines before the webview.loadUrl so that you can obtain something like this :
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
webView = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
super.loadUrl("file:///android_asset/www/index.html");
Finally, refresh the Android's project and relunch the app.

Adding CookieManager to android Childbrowser

I am trying to get local cookies (file://) to work on android 3.1+
within ChildBrowser. I found a blog response talking about this
specific issue and how to remedy it with Cookie Manager. I can't
figure out where in the plugin to put the code. Has anyone
successfully implemented this?
Comment Below from http://code.google.com/p/android/issues/detail?id=3739
Comment 16 by edtechk...#gmail.com, Feb 1, 2012
I got this thing working, for Android 2.2, javascript's
document.cookie works fine, just make sure that in your
Webview...javascript is enabled like so:
yourWebViewVariable.getSettings().setJavaScriptEnabled(true);
for Android 3.1 just add this to your java file onLoadInit:
CookieManager.setAcceptFileSchemeCookies(true); //This is the line that specifically makes it work so the other lines is optional
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();
For those interested, I figured it out. It is not a Childbrowser issue at all. You have to make the parent Phonegap project accept local cookies and then Childbrowser will too.
To do so, You should have a file called youappname.java in your PhoneGap project, probably with this contents or similar:
import android.os.Bundle;
import org.apache.cordova.*;
public class App extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
Modify it to look like this example:
import android.os.Bundle;
import android.webkit.CookieManager;
import org.apache.cordova.*;
public class App extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
CookieManager.setAcceptFileSchemeCookies(true);
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}

Categories

Resources