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();
}
Related
I'm displaying a webpage in my Android app using a WebView, this webpage triggers a window message event and gives some data through that event. I need to access the data from the message event.
To achieve this, I'm adding the JavaScript event listener by using evaluateJavascript() method and trying to call a Java function from the JavaScript using addJavascriptInterface().
My Code so far:
public class MyActivity extends AppCompatActivity {
#Override
#SuppressLint("SetJavaScriptEnabled")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.addJavascriptInterface(new JsObject(this), "Android");
webView.evaluateJavascript("window.addEventListener('message', (e) => {Android.logData('Test String')})", null);
webView.loadUrl("https://example.com/mypage");
setContentView(webView);
}
private class JsObject {
Context context;
JsObject(Context context) {
this.context = context;
}
#JavascriptInterface
public void logData(String s) {
Log.v("EEE", s);
}
}
}
But the event handler is not getting called. I'm not sure whether this is the right approach. Please help. TIA.
Switch the order of your loadUrl and evaluateJavascript calls. The loadUrl is resetting that state. Note: this may require changing the code on the page to avoid a race condition triggering the message.
From the evaluateJavascript docs:
global variables and functions defined before calling loadUrl(java.lang.String) will not exist in the loaded page
I have a native android app in which there are two activities, 1st activity contains a button and on action of button I want to start the 2nd activity. Content of seconds activity will be from a website. Is it possible to make the 2nd activity behave like webapp ?
If I use WebView in this case, it is displaying the website inside the activity but when I try to interact with the website, it ask to open it in the browser which I don't want. I want to interact with the website within the activity.
Any help will be highly appreciated.
Thanks a ton.
Use below code... for open link in WebView itself instead of browser...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
WebView engine = (WebView) findViewById(R.id.web_view);
engine.loadUrl("https://play.google.com/store?hl=en");
engine.setWebViewClient( new HelloWebViewClient() );
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
return false;
}
}
see this link for more details... WebView should not open links in browser
I think if you set the baseURL property when creating the Webview, you'll get what you want.
Create you're own Webview where you can override the default behaviour:
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
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");
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" />
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");
}
}