0

I am using intent along with switch case two load two different URL using same webview. It worked fine but now how to load two different urls after Interstitial ads shows up. At this point of time intersitial ads show ups and after closing it I am getting a blank webview intead of buttons respective url.

Can anyone modify my code.

Home activity.

enter code here
public class home extends AppCompatActivity {
AdView mAdview3;
private InterstitialAd interstitialAd;
public RatingBar ratingbar;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ratingbar = (RatingBar) findViewById(R.id.ratingBar);
    Button button = (Button) findViewById(R.id.button3);

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");     //Main_app id
    mAdview3 = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
    mAdview3.loadAd(adRequest);

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    AdRequest adRequest1 = new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
    interstitialAd.loadAd(adRequest1);
    interstitialAd.setAdListener(new AdListener() {
        public void onAdClosed() {
            startActivity(new Intent(home.this, playerzpot_team.class));
        }
    });


    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (interstitialAd.isLoaded()) {
                interstitialAd.show();
            } else {
                startActivity(new Intent(home.this, playerzpot_team.class));
            }
        }
    });
}


public void click(View view) {

    Intent intent = new Intent(getApplicationContext(), playerzpot_team.class);
    switch (view.getId()) {
        case R.id.button3:
            intent.putExtra("url", "http://dream11expertteams.com/");
            startActivity(intent);
            break;

        case R.id.button2:
            intent.putExtra("url", "https://nikhilsfantasy.wordpress.org");
            startActivity(intent);
            break;

        default:
            break;
    }
}

}

And here goes my second activity,

enter code here

public class playerzpot_team extends AppCompatActivity {
    private WebView aboutus_myWebView;
    AdView mAdview5;
    String url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playerzpot_team);

        String url = getIntent().getStringExtra("url");
        aboutus_myWebView = (WebView) findViewById(R.id.playerzpot_webview);

        aboutus_myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = aboutus_myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        aboutus_myWebView.loadUrl(url);


        MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");       
        mAdview5 = (AdView) findViewById(R.id.adView2);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
        mAdview5.loadAd(adRequest);

}

    @Override
    public void onBackPressed()  
    {
        if (aboutus_myWebView.canGoBack()) 
        {
            aboutus_myWebView.goBack();
        }   
        else  
        {
            super.onBackPressed();
        }
}

}

Can anyone help me by modifying code. I want to show interstitial ads before webview gets loaded.

Waiting for help ....

1 Answers1

0

You just create different onClickListeners for buttons and then pass url as intent. You don't need two activities. Just get url from intent in the activity and pass to the webview.

Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);

    button1.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
         Intent intent = new Intent(MainActivity.class, SecondActivity.class); // use the names of your activities
         intent.putExtra("url", url1);
         startActivity(intent);
      }
    });
 button2.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
         Intent intent = new Intent(MainActivity.class, SecondActivity.class); // use the names of your activities
         intent.putExtra("url", url2);
         startActivity(intent);
      }
    });

Then to get string in WebActivity use

String url;
Bundle extras = getIntent().getExtras();
if(extras == null) {
        url= null;
} else {
        url= extras.getString("url");
}
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • Thank you for reply but you are pre asumming too many things here, I am new in developing apps. And I need a example or sample file which use single webview to access different url with help of buttons. Because in sample file, will have all the included files described. If i use above code , i face lots of errors. – Nikhil Komalan Mar 13 '18 at 18:38
  • @NikhilKomalan this site works this way....you google sample app, implement it , potentially face problems and then ask a question with your code which has problems in implementation you faced. If your question is ...I need a sample app. People won't be able to "answer" it – Rainmaker Mar 13 '18 at 18:42
  • Help me in this....https://stackoverflow.com/questions/49328288/how-to-show-up-interstrial-ads-while-opening-two-different-url-in-webview-with – Nikhil Komalan Mar 17 '18 at 13:12