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();
}
}
}
}
Related
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());
}
I created a little android app to view a responsive website. I have implemented a pull refresh and it works, however when pulling on refresh it redirect to the home page I put in the LoadUrl. How to refresh by reloading the same url the user is visiting ?
My java code:
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb()
{
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
//tiga baris dibawah ini agar laman yang dimuat dapat
//melakukan zoom
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
//baris dibawah ini untuk menambahkan scrollbar didalam webview-nya
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("http://example.com");
swipe.setRefreshing(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
webView.loadUrl("file:///android_asset/error.html");
}
#Override
public void onPageFinished(WebView view, String url) {
//ketika loading selesai, ikon loading akan hilang
swipe.setRefreshing(false);
}
});
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (webView.getProgress()==100){
swipe.setRefreshing(false);
}else {
swipe.setRefreshing(true);
}
}
});
}
}
Thanks in advance
In Swipe Refresh layout call webView.reload();
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
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);
}
}
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();
}
}