Search code examples
androidandroid-webview

How to multi urls in single webview


I am new to webView ,here I had t onclick event when I click text one and open in webview and when I click text 2 open with in same webview any one please how to place two onclick with in same webview ,every textview string come from server I tried this way but no use any ne please help me I search I google but their is no use

Here below my code

Activity.java

 //webview onclick and get bundle
    webviewurl=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl();

    webviewurl2=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl2();

    news_site_link_one=(TextView)findViewById(R.id.news_SourceLink_text_one_t_webview);
    news_site_like_two=(TextView)findViewById(R.id.news_SourceLink_text_two_t_webview);

  news_site_link_one.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View v) {
        Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
       webviewintent.putExtra("webviewurl", webviewurl);
         startActivity(webviewintent);

    }
   });


    news_site_like_two.setOnClickListener(new View.OnClickListener() {
     @Override
      public void onClick(View v) {
        Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
          webviewintent.putExtra("webviewurl2", webviewurl2);
      startActivity(webviewintent);


     }
   });

here my webview code

 String SourceURL;
WebView webview;
final Activity activity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news__web_view);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    setContentView(R.layout.activity_news__web_view);

    Intent intent =this.getIntent();

    if(intent!=null)


        SourceURL =  intent.getStringExtra("webviewurl");
    if(SourceURL.equals("webviewurl")) {

        webview = (WebView) findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);
    }

    if(SourceURL.equals("webviewurl2")) {

        webview = (WebView) findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);
    }





    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    webview.loadUrl(SourceURL);
}

Solution

  • Use containsKey

    Bundle data =getIntent().getExtras();
    
    if(data.containsKey("webviewurl"))
    {
        SourceURL =data.getString("webviewurl");
    }
    else if(data.containsKey("webviewurl2"))
    {
                    SourceURL =data.getString("webviewurl2");
        }