How to able javascript in Android WebView? - java

I've got this short code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("https://onet.pl");
mywebview.setWebViewClient(new WebViewClient());
}
How to able here javascript? I tried various version from stackoverflow, but nothing worked for me, i got a lot of errors. Anyone can help me?

Related

Android Studio variable cannot be found WebView

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import static com.example.tencho.cst.R.id.*;
public class MainActivity extends AppCompatActivity {
private WebView MywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MywebView =(WebView)this.findViewById(WebView);
WebSettings webSettings=MywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
MywebView.loadUrl("www.cst.edu.bt");
}
}
This is the MainActivity.java. it's always giving me an error cannot find symbol variable WebView.
I did read the past similar post but I couldn't solve it error. Please need help!
At this line
MywebView =(WebView)this.findViewById(id);
id should be like R.id.webview1
webview1 is the id in your XML layout
The error is on this line:
MywebView =(WebView)this.findViewById(WebView);
The cause of this error is that the generated R.id class has no field named WebView (the argument you are passing to the findViewById() method). This is probably happening because your layout's <WebView> tag is missing an id attribute. Add this line to your <WebView> tag:
android:id="#+id/WebView"
Ben's answer is correct. In addition, if your new to Android, if you added this part in your tag which can be found in your activity_main.xml layout..
android:id="#+id/webview"
then in your MainActivity.java, it should be like this:
MywebView =(WebView) findViewById(R.id.webview);

Error: "error:cannot resolve symbol 'WebViewClient'

I already tried referring both to this StackOverFlow question as well as this tutorial but still have not found a solution to my problem.
I set permissions already in the Android Manifest XML file, and have my webview problem under the FullscreenActivity file. My code is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
WebView view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new WebViewClient());
view.loadUrl("platinumskincare.com");
view.getSettings().setJavaScriptEnabled(true);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
The error I keep getting says 'Cannot Resolve Symbol 'WebViewClient', and I believe it is on line 44 of my code. What should I do and how do I fix this problem?
Check if you have these imported
import android.webkit.WebView;
import android.webkit.WebViewClient;
Clean and rebuild your project in Android Studio.

Loading a web page in Android App

I don't know how to access my index.html webpage when the app is loaded. Actually on my code I use file:///android_asset/index.html, I've tried to load a Google page to test, it works, but it launch the Chrome app...
Here is my code of my MainActivity.java :
package com.totominc.fr.webapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/index.html"); // error may be here...
}
}
But the error which I got is that it can't find the file index.html.
Here is a screenshot of my project.
Thanks for helping me!
try this
file:///Android_res/drawable/index.html
As your index html is in drawable folder. this would work
You need to put your index.html file into your assets folder, and not /res/drawable.
So your index.html should be in /assets/index.html.
Create your assets folder in src/main/assets/ and put your index.html there, not inside your drawables. Regarding the page opening in Chrome, try this instead
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
// Changed here
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file:///android_asset/index.html"); // error may be here...
}

webView.loadUrl is not working

This is my code:
package sai.datla.game;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class GametestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.clearSslPreferences();
webView.loadUrl("file:///android_asset/www/index.html");
}
}
but it is saying that it cannot find the link in the emulator. I have checked many times if my index.html file is in the www folder, which is in my assets, but it won't work. Please help.
By the way I am only twelve years old so please make the answers easy to understand for a child.
// Find view in layout
WebView wv = (WebView) findViewById(R.id.webView_tobe_loaded);
// Get settings
WebSettings wbset = wv.getSettings();
// Enable JavaScript
wbset.setJavaScriptEnabled(true);
// Set new client (to handle website in your app)
wv.setWebViewClient(new MyWebViewClient());
// Example of the URL
String url = "http://www.google.com";
// Load website
wv.loadUrl(url);
This code will help you to solve the problem.
It worked for me.
instead of loadUrl, try using the loadDataWithBaseURL method:
webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");

Open link from Android Webview in normal browser as popup

I have a simple webview which loads a page. This page has a few links that opens within the webview. That's what it supposed to do, so it's working all fine.
But there is one single link from that page which should load as a popup, so I want it to open in the normal browser when people click it. But as I stated, all links are opening in the webview, so that link does it also.
My question is, how can I make this link open in the normal browser as a kind of a popup? Is it even possible? The link is variable so it's changing always, it cannot be hardcoded within the application to open in a new browser browser.
Is it possible and how can I do it?
Here's an example of overriding webview loading to stay within your webview or to leave:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class TestWebViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
}
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somePartOfYourUniqueUrl")){ // Could be cleverer and use a regex
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
} else {
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
}
public class WebViewActivity extends Activity {
private WebView webView;
private ProgressDialog progress;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("https://www.example.com");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}

Categories