PhoneGap: How do I get appView's id and pass it around? - android

For PhoneGap Application, as the instruction said, I have replaced the setContentView() line with super.loadUrl("file:///android_asset/www/index.html"); and the next line is appView.addJavascriptInterface( new JavaScriptInterface(this), "Android");
My question is
if I wanna use appView in my JSInterface, how can I find it, or pass it into JSInterface, or reference it from JSInterface.
And is appView the only webview in PhoneGap?

First you should not take the approach of adding your own JS interface. It is better if you write a PhoneGap plugin:
http://docs.phonegap.com/en/2.0.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android
We've already suffered through all the pains of making sure the JS interfaces are setup correctly. From your plugin you can get access to appView by using:
this.webView
Yes, appView is the only webview in PhoneGap.

Related

why is (WebView) in brackets in android

My background is mainly web design using JS HTML and CSS writing simple apps - nothing professional. I have recent had the urge to learn a bit about Android development. I quickly came across the below line. I understand defining the variable as a webview etc, but I don't understand why '(WebView)', in brackets, is used in the statement.
WebView browser;
browser=(WebView)findViewById(R.id.webkit);
I can only imagine its something to do with android as I haven't come across it in JS before. Any help or info on where I can read up on this would be much appreciated!
It is type casting.
WebView browser;
In the above line, you are declaring browser as an object of WebView.
So it can hold only WebView
So, in the below line, you are setting the type to WebView in order to prevent error if the findViewById(R.id.webkit); return otherwise
browser=(WebView)findViewById(R.id.webkit);
This is actually a Java feature.
You can read more about it in Oracle Java Documentation
That is what we call as type casting!
Code:
WebView browser;
browser = (WebView) findViewById(R.id.webkit);
Explanation:
You are assigning value to the instance of WebView(i.e., browser) by typecasting the View from your design(.xml file) to WebView. Because, .java file should recognize what view is the webKit in the design.

Android WebView not loading local HTML files

I'm using the standard Android SDK with Ecliple as provided from the Android website.
What I want to to is to have an activity display a WebView that loads a local HTML-file. Everything works fine until I try to load the webview.
I've placed all the index-files I've tried in the project's assets-folder.
This is the code in my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_assets/www/index.html");
setContentView(webView);
}
When I run the app over ADB, I get the following message in the WebView:
Webpage not available
The webpage at file:///android_assets/www/index.html might be temporarily down or it may have moved permanently to a new web address.
I know this problem has been posted before, and I've tried every single solution out there. But it just won't load. It displays online http-addresses just fine, but it doesn't seem to find any of the local files.
Please help me, this drives me mad.
Change this line:
webView.loadUrl("file:///android_assets/www/index.html");
to this :
webView.loadUrl("file:///android_asset/www/index.html");
is android_asset not android_assets :D. Good Luck

Android - Get current webview for setWebViewClient()

After times of searching, I could not find the result for this:
My browser application is using webview, I need to use this function: currentwebview.setWebViewClient(). But I do not know how to get the current webview to apply that function.
I mean when I do not have the current webview id, then is there a way to get current webview for using setWebViewClient() ??
Thank you for your help !!
findViewById same as you would any view.
WebView currentWebView = (WebView) findViewById(R.id.my_web_viewId);

Using PhoneGap external url but Cordora js not working in Android

I'm facing a problem when I create a program by PhoneGap.
in my program, I'm using a local index.html file in my main activity, after the index.html loaded, I'm using window.open to redirect my page to an external page from the other server.
window.open("http://192.168.0.11/test.html", '_self');
now the page redirect to test.html, cordova.js was included in test.html:
//included the cordova js file in test.html file
<script src="js/cordova.js"></script>
in test.html, I called the the "exitApp" method of cordora as following:
function onExit()
{
navigator.app.exitApp();
}
however, I saw the error message as following in ADT logcat:
Uncaught TypeError: Cannot call method 'exitApp' of undefined
but this file is working when I call it by local e.g. file:///adnroid_asset/www/test.html
anyone can help me on this? thanks in advance.
Don't use window.open. Basically it'll try to open it in a new window and you'll lose the reference to Cordova objects. Use:
window.location.href = "http://192.168.0.11/test.html"
Which will simply load the give url in the same page, keeping the Cordova objects.
More details on the differences here.

passing data from javaclass to html webview

I'm using a webview in my app.
now what I want to do is to pass the data from my javaclass to html.
fro example I have a textview in my android javaclass and when I hit the button send the webview will show with html saying Hi + the name that I input in textview.
is it possible?
Check Using WebViews may be it will help
You can generate your html and then load it to WebView using loadData method for example.
Or you can use addJavascriptInterface to invoke methods of some Java object from javaScript code in WebView
See below code
WebView webview = (WebView) findViewById(R.id.webview);
webview.loadUrl("http://www.xxx.com/web/apps/jots.do");

Categories

Resources