Closing ad popup window on webview not working onBackPressed - java

I'm trying to close the ad popup window on webview onBackPressed. But it keeps reloading the ad window. I am using exoclick and juicyads popunders on the website.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
WebView view = findViewById(R.id.webview);
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
view.setSystemUiVisibility(uiOptions);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setSupportMultipleWindows(true);
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.setWebViewClient(new WebViewClient() {
view.loadUrl(url);
return false;
});
view.loadUrl("https://example.com");
}
#Override
public void onBackPressed() {
WebView view = findViewById(R.id.webview);
if (view.canGoBack()) {
view.goBack();
return;
} else {
moveTaskToBack(true);
}
}
}

Related

Unable to open Google Maps link in Android web view app

I have created a webview app of my site in Android Studio. Could someone please look into the code and solve the issue. This is my code:
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
} catch (Exception e) {
return true;
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("https://xyz.in/admin_login.php");
}
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
When WhatsApp icon is clicked, it's opening in WhatsApp application,but when view location is clicked, the button is not responding. Please help on this issue.
Please find the attached image for your reference

How to exit web view to the last fragment?

I am trying to exit a web view called on the click of a fragment's element.
Here's my code:
public class MainActivity extends AppCompatActivity {
private WebView webView;
private CustomPagerAdapter customPagerAdapter;
private ViewPager2 viewPager;
TabLayout tabLayout;
static MainActivity activity;
static MainActivity activity;
public static MainActivity getInstance(){
return activity;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
setContentView(R.layout.main_activity);
viewPager = (ViewPager2) findViewById(R.id.pager_main);
tabLayout = (TabLayout) findViewById(R.id.tab_layout_main);
setPagerAdapter();
customPagerAdapter.addFragment(new GoogleFragment());
customPagerAdapter.addFragment(new YouTubeFragment());
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) -> {
if (position == 0) {
tab.setText("Google");
} else if (position == 1) {
tab.setText("YouTube");
}
}
).attach();
}
private void setPagerAdapter(){
customPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), getLifecycle());
viewPager.setAdapter(customPagerAdapter);
}
public void setWebView(String url) {
setContentView(R.layout.web_view);
webView = (WebView) getInstance().findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
finish();
}
}
}
On pressing back button, instead of exiting the web view, the app exits itself. The webView.goBack() works fine. But clicking back button from the main url, exits the app.
Any help is appreciated!

Webview loading url but opening content in default browser?

It opens my link in Webview but when I try to open hyperlinks and content of the web or navigation link, it starts opening with the default browser.
public class MainActivity extends Activity {
private WebView mWebView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://");
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Objects.requireNonNull(Uri.parse(url).getHost()).endsWith("http://www.ieltsmadeeasy.tk/")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I want to browse the whole website inside the web view, not any browser please help me
Here is some documentation about WebViews: https://developer.android.com/guide/webapps/webview#java
The problem is, that you don't override the shouldOverrideUrlLoading(view, url) function of the WebViewClient, as you use the default one.
If you use this as the WebViewClient, it should work (copied from examples in documentation):
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 website, 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;
}
}
So, just take this class and change the line mWebView.setWebViewClient(new WebViewClient()); to mWebView.setWebViewClient(new MyWebViewClient());

Open new tab in same page with Android WebView

I am making an app for WebView with a header progress bar. It is working nicely but after loading the web page, if I click any link on the webpage, the app closes and opens the default browser. But instead, I want to stay in the WebView page after the click and open the new tab on the same page.
How can I do this?
public class MainActivity extends Activity {
private WebView webView;
private EditText urlEditText;
private ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// urlEditText = (EditText) findViewById(R.id.urlField);
webView = (WebView) findViewById(R.id.webView);
//webView = new WebView(this);
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
webView.setWebChromeClient(new MyWebViewClient());
//Button openUrl = (Button) findViewById(R.id.goButton);
String url = "http://amrtube.com/result/index.php";
if (validateUrl(url)) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptEnabled(true); // enable javascript
this.webView.getSettings().setAllowFileAccess(true);
this.webView.getSettings().setBuiltInZoomControls(true);
//this.mWebview.getSettings().setSupportZoom(true);
final Activity activity = this;
this.webView.setScrollBarStyle(33554432);
this.webView.setScrollbarFadingEnabled(false);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(url);
MainActivity.this.progress.setProgress(0);
}
}
private boolean validateUrl(String url) {
return true;
}
private class MyWebViewClient extends WebChromeClient {
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.toString());
return true;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
Replace your line with this
this.mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);

android: WebView canGoBack() Error

I'm following (Building Web Apps in WebView) tutorial, but I have problem with (Navigating web page history) I'm runing it on my device and I click on a link but when I click the back button the application shuts down:
That's my activity:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("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;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView myWebView = null;
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
public class MainActivity extends Activity {
WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("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;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}

Categories

Resources