Show Interstitial Ad On Button Click In WebView - android

i make apps of my Wordpress websites for android and i use WebView in my apps, i want to show Interstitial Ads in my app but when users click on Next Page Button Which is coded in my website, my next page button is like this
<?php next_posts_link( '<span class="nextpost"> '.__('Next', 'wp-mobile-edition').' ›</span>' );
i want to show Interstitial ad when user click on next button.
how it can be ? thanks

Try below code
#SuppressLint("SetJavaScriptEnabled")
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.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setWebViewClient(new MyBrowser());
mWebView.loadUrl("file:///android_asset/index.html");
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.addJavascriptInterface(new Object()
{
#JavascriptInterface
public void performClick() throws Exception
{
Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
// implement your logic here for show Add
}
}, "login");
return true;
}
}
}

Related

How can I block ad/popup/domain

How can I block ad/popup/domain
Hello, I'm making a simple webview and I would like that in this webview I could limit which domains my user can access, for example, I would like only google.com and youtube.com to be accessed, how can I do this?
Is this the best way to block popup and advertisement?
public class MainActivity extends AppCompatActivity {
WebView wv;
public void onBackPressed(){
if(wv.canGoBack()){
wv.goBack();
}else{
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView);
//Enable Javascript
wv.getSettings().setJavaScriptEnabled(true);
wv.setFocusableInTouchMode(true);
// set Render Priority to high
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// Load url
wv.loadUrl("https://www.google.com/");
wv.setWebViewClient(new myWebViewClient());
}
String currentUrl;
private class myWebViewClient extends WebViewClient{
#Override
public void onLoadResource(WebView view, String url) {
Log.e("Resource ",url);
}
}
}
Before calling loadUrl, just perform checks on the String. If it is anything other than you want, display something like a snackbar with the message

How to get url after loading page in webview

I need to take url after loading a web page in my web view to check condition and if I use onPageFinished() method, then where to use.
Just set a webview client and override onpagefinished as shown below
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(), url,Toast.LENGTH_LONG).show();
}
});
}
}
inside onpagefinished you have the url

Android open new activity in webview is not successful - shouldOverrideUrlLoading is not working

I want to do an app that is mainly base on webview. MainActivity is to load www.example.com/products.php.
www.example.com/products.php - display all products*
www.example.com/products.php?id=123 - display individual product base on product ID
www.example.com/cart.php - when clickcing on "Add to cart" on product.php?id=123, it will not open a new Activity but remain in the same activity.
MainActivity.java
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView);
WebSettings wss = wv.getSettings();
wss.setJavaScriptEnabled(true);
wv.loadUrl("http://www.example.com/product.php");
wv.setWebViewClient(new WebViewClient());
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com/cart.php")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}else{
// 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 void onBackPressed() {
if(wv.canGoBack()){
wv.goBack();
}else{
super.onBackPressed();
}
}
And also for "shouldOverrideUrlLoading " , it shows depreciated... so what's the new name for this?

How to get Android WebView Link Click URL

I want to show the URl of a video in a TOAST, each time the user click on a video. I get this toast shown only once when I Launch an app first time. Here is my Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview=(WebView)findViewById(R.id.webView);
mywebview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Toast.makeText(MainActivity.this,url, Toast.LENGTH_SHORT).show();
return true;
}
});
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.youtube.com");
}

WebView fails to load webpage

I have a WebView trying to load a WebPage like this:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv=(WebView)findViewById(R.id.webview);
WebSettings ws=wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
}
I have added the internet permission to the manifest
<uses-permission android:name="android.permission.INTERNET"/>
However the WebView is blank,it neither throws an error nor loads the web-page.How do I load webpages like this,I have only tried loading local html files before and I would like to know if I have to do something different.
Try like this way:
mWebView.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
mWebView.setWebViewClient(new HelloWebViewClient());
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Use this code .shouldOverrideUrlLoading() not use compulsary.use this code and please reply me this code work or not
Try below code.
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
setContentView(mWebview );
}
}

Categories

Resources