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();
}
}
Related
I have placed an interstitial with click on back button in my Web View app. However , back button doesn't work when ad is not available. However, it seems to work fine after an interstitial ad has been loaded once. I am not a professional developer. Any help is much appreciated. Thank you.
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//admobinterstital
MobileAds.initialize(this, new OnInitializationCompleteListener() {
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
public void onAdLoaded(InterstitialAd interstitialAd) {
InterstitialAd unused = MainActivity2.this.mInterstitialAd = interstitialAd;
Log.i("TAG", "onAdLoaded");
}
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.i("TAG", loadAdError.getMessage());
InterstitialAd unused = MainActivity2.this.mInterstitialAd = null;
}
});
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
});
//WEB VIEW SETTINGS
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
//LIST VIEW
int position = getIntent().getIntExtra("story_key", 0);
//FOR MORE URL
}
// ENABLE BACK PRESS
public void onBackPressed() {
InterstitialAd interstitialAd = this.mInterstitialAd;
if (interstitialAd != null) {
interstitialAd.show(this);
if (this.webView.canGoBack()) {
this.webView.goBack();
} else {
super.onBackPressed();
}
}
}
}
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);
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
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;
}
}
i was converting my website and my youtube channel in app
this app have two activity haves two different webview when click button for second webview then unfortunately stopped . and second activity have almost same coding
{package com.example.cybercry.cybercry;
private InterstitialAd interstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pubxxxxxxxxxxxxxxxxxxxxxx");
AdView ads=(AdView) findViewById(R.id.adView);
AdRequest adRequest= new AdRequest.Builder().build();
ads.loadAd(adRequest);
prepareAds();
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
#Override
public void run(){
Log.i("hello", "world");
runOnUiThread(new Runnable() {
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Log.d("TAG"," Interstitial not loaded");
}
prepareAds();
}
});
}
}, 30, 60, TimeUnit.SECONDS);
myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://");
myWebView.setWebViewClient(new WebViewClient());
}
public void onclick()
{ Intent cintent = new Intent(MainActivity.this,webiste.class);
startActivity(cintent);
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
public void prepareAds(){
interstitialAd= new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxxx");
AdRequest adRequest= new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
}
}