android phonegap webview - android

hello all i am very new to phonegap.. i want to webview into my application how can i add this? i've created same app. using android but how it can devleope using PHONEGAP?
private WebView mWebView;
//bla bla bla..
#Override
public void onCreate(Bundle savedInstanceState) {
mWebView = (WebView) findViewById(R.id.webviewHelp);
WebSettings webSettings = mWebView.getSettings();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyjavascriptInterface(), "HTMLOUT");
mWebView.loadUrl(strURL);
mWebView.setWebViewClient(new HelloWebViewClient());
}
public class MyjavascriptInterface {
public void showHTML(String html)
{
bla bla bla...
}
}
public class HelloWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
bla bla bla...
}
}
}
thanks in advance :Pragna

For your most basic PhoneGap app you should extend your main activity from DroidGap.
import com.phonegap.DroidGap;
public class Main extends DroidGap {
/** Called when the activity is first created. */
//private Button m_button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}

I would suggest checking out this link for a series of tutorials using Phonegap within the Eclipse development environment. Once you are setup on eclipse, you only need to overide a few lines of code in the Android Activity class to call your web pages. Phonegap takes care of the rest in terms of opening up a webview class and rendering your html within it. You will no longer to code that yourself (as you've done above). The tutorials spell it out quite clearly. You can also add your own custom JavaScript interface methods as well. Again that's described in the tutorials. Hope this helps.

In your case there is a need to Embedding Cordova WebView on Android here is link for it
Modify your main Activity as
public class MainActivity extends Activity implements CordovaInterface {
CordovaWebView cwv;
/* Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cwv = (CordovaWebView) findViewById(R.id.tutorialView);
cwv.loadUrl("file:///android_asset/www/index.html");
}
And in your layout replace your webview with
<org.apache.cordova.CordovaWebView
android:id="#+id/tutorialView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Related

loading division of webpage in android webview

My webpage is divided in various parts instead of loading full page, i want to load a part of that page div id = map_canvas.
Below code is giving me full page but i want only one part map_canvas
public class Jetbill extends Activity {
public WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jetbill);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://jetbill.biz/users/login.php");
}
}

Loading file from assets folder to webview

I'm trying to load a html file in my webview using this codes. but is show Webpage not available
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.frux.web.R.layout.activity_main);
WebView web = (WebView) findViewById(R.id.web1);
web.loadUrl("file:///android_assets/www/webpage");
}
}
I already edit my codes into this but still webpage not available
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web = (WebView) findViewById(R.id.web1);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file://android_assets/www/index.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I just mistype the assets. It should be "asset" without "S".
The file should be extension with html
The easiest way would probably be to put your web resources into the assets folder then call webView.loadUrl("file:///android_asset/www/filename.html");
try this will help you...
web.loadUrl("file:///android_assets/www/webpage");
webpage is a folder or a file? I guess it should be
web.loadUrl("file:///android_assets/www/webpage/file.html");
or
web.loadUrl("file:///android_assets/www/webpage.html");
Write below line
setContentView(R.layout.activity_main);
web.loadUrl("file:///android_assets/www/webpage/index.html");
instead of
setContentView(com.frux.web.R.layout.activity_main);
web.loadUrl("file:///android_assets/www/webpage");

Android App with PhoneGap: InvocationTarget Exception by calling addJavascriptInterface

I am using the Android SDK and PhoneGap to create a native Android App. Now I want to use Java methods in the view, calling by JS methods.
In the Main Class I called the "addJavascriptInterface" method to bind a java class with the view.
public class App extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
super.loadUrl("file:///android_asset/www/index.html");
}
}
The problem is I get an InvocationTarget Exception when the programm executes the line "appView.addJavascript..." and the program crashes on the device.
Any solutions here?
Thanks!
Calling Java methods from JavaScript is exactly what PhoneGap plugins enable. Checkout several examples here
Using addJavascriptInterface before super.init(); can make the application crash.
You should try the following :
public class App extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
super.loadUrl("file:///android_asset/www/index.html");
}
}

Image from WebView

i m getting a photo from the web,but i see it very large..how could i see it with zoom out?this is my code for webView:
public class gavros extends Activity {
WebView browser;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.efimerides);
browser =(WebView)findViewById(R.id.webview);
browser.loadUrl("http://...");
}}
Checkout WebView's setInitialScale method:
http://developer.android.com/reference/android/webkit/WebView.html#setInitialScale(int)

Need help with android WebView

I am new to Android and java development. I create a WebView object in the OnCreate function. Now I need to be able to pass this same object to other functions in the code, such as the onOptionsItemSelected function. I currently have it where I just create a new WebView object in each function where I need it, but this slows down the code since it has to recreate it and such.
Something as simple as this?
public class MyActivity extends Activity {
WebView wv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv = new WebView(this);
setContentView(wv);
#Override
public boolean onOptionsItemSelected(MenuItem item){
wv.doSomething();
}

Categories

Resources