How to fix 'cannot find symbol variable MyWebViewClient' - java

I'm getting error: cannot find symbol variable MyWebViewClient.
public class MainActivity extends AppCompatActivity {
WebView myWebView = (WebView) findViewById(R.id.webview);
#SuppressLint("JavascriptInterface")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView.loadUrl("https://*********.com"); //Just removed for security
WebSettings ws = myWebView.getSettings();
ws.setJavaScriptEnabled(true);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
}
else{
super.onBackPressed();
}
}
}
and my MyWebViewClient class
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://*********.com")) {
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));
return true;
}
}
(Imported all necessary header files as instructed by Android-studio)
But I'm getting error: cannot find symbol variable MyWebViewClient.
Please someone help me.

It looks like you are trying to pass your MyWebViewClient class as an instance in this line :
myWebView.setWebViewClient(MyWebViewClient);
maybe try something like this :
myWebView.setWebViewClient(new MyWebViewClient());
or
MyWebViewClient myWebviewClientInstance = new MyWebViewClient();
myWebView.setWebViewClient(myWebviewClientInstance);

Related

pass a link in a webview

I am trying when someone click for example : eee.com , to open it in my Webview app
public class MainActivity extends AppCompatActivity {
public static WebView webView;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.mainWebview);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.eee.com/");
webView.setWebViewClient(new WebViewClient(){
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("eee.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
});
}
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
webView.loadUrl(uri.toString());
}
but the app crashes on open it right away , I tried this : :
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if(getIntent().getData() != null){
webView.loadUrl(uri.toString());
}
}
app works but on link click it just open the app without passing the link
I solved it my self with :
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Uri x = intent.getData();
webView.loadUrl(x.toString());
}

How to do a second Webview in Android Studio

inside my Webview App I want to call a second website after a quick pause.
Unfortunately it looks like the pause between the first and the second link dont work...
I dont know why can u give me a tip pls :)
public class MainActivity extends AppCompatActivity {
private WebView webView;
private WebSettings Websetting;
private WebView webView2;
private WebSettings webSettings2;
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
Websetting = webView.getSettings();
Websetting.setJavaScriptEnabled(true);
Websetting.setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://google.de");
webView.clearHistory();
try {
Thread.activeCount();
Thread.sleep(15000); //1000 milliseconds is one second.
webView2 = findViewById(R.id.webView);
Websetting = webView2.getSettings();
Websetting.setJavaScriptEnabled(true);
Websetting.setLoadWithOverviewMode(true);
webView2.loadUrl("https://google.com");
webView2.clearHistory();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
ยดยดยด
Try using Postdelayed method :
public class MainActivity extends AppCompatActivity {
private WebView webView;
private WebSettings Websetting;
private WebView webView2;
private WebSettings webSettings2;
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
Websetting = webView.getSettings();
Websetting.setJavaScriptEnabled(true);
Websetting.setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://google.de");
webView.clearHistory();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
webView2 = findViewById(R.id.webView);
Websetting = webView2.getSettings();
Websetting.setJavaScriptEnabled(true);
Websetting.setLoadWithOverviewMode(true);
webView2.loadUrl("https://google.com");
webView2.clearHistory();
}
}, 15000);
}
}
}
You need to change the following code within the run braces
webView2 = findViewById(R.id.webView2);

Webview set overrideurl not in use

Im trying to use the shouldOverrideUrlLoading to handle some tel: links on my webview, but i get this error:
error: method does not override or implement a method from a supertype
How can i fix that?
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.WebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://**************/");
myWebView.setWebViewClient(new WebViewClient());
final ProgressBar Pbar;
Pbar = (ProgressBar) findViewById(R.id.pB1);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
myWebView.requestFocus(View.FOCUS_DOWN);
}
}
#SuppressWarnings("deprecated")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
view.reload();
return true;
}
view.loadUrl(url);
return true;
}
});
}
}
the override is grayed out but i cant figure how to fix it, im not very familiar with java and its driving me crazy, thanks in advance!
This is because there is no shouldOverrideUrlLoading in the WebChromeClient that you are trying to override.
You can override the shouldOverrideUrlLoading of WebChromeClient as it is present in it

Prevent Double Tap on Android WebView

I have a fullscreen WebView and I want to;
If my user double tap the screen, prevent second tap.
I am very very new on Android, please be very clear. I pasted my codes;
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// REMOTE RESOURCE
mWebView.loadUrl("https://www.example.com");
mWebView.setWebViewClient(new MyWebViewClient());
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
MyWebViewClient.java
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}

onBackPressed() - cannot resolve symbol 'webView'

I'm trying to get back button functionality in my WebView app, but the onBackPressed() function is giving the error 'cannot resolve symbol webView'
Here's my code:
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView;
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
// To keep browser in width of phone
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
// Sets default text size
webSettings.setTextZoom(175);
MyWebViewClient webViewClient = new MyWebViewClient();
webView.setWebViewClient(webViewClient);
webView.loadUrl("http://www.google.com");
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
You can do this in onBackPressed:
#Override
public void onBackPressed() {
WebView webView = (WebView) findViewById(R.id.webview);
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
Edit:
You can do this (with field):
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState){
// ...
this.webView = (WebView) findViewById(R.id.webView);
// ...
}
#Override
public void onBackPressed(){
// now you don't need findViewById because you can access field prepared in onCreate
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}

Categories