Search code examples
javaandroidbuttonandroid-webview

How to open a url in webview on new screen based on button click


Hi i have tried all available solutions in stackoverflow but nothing is working, app is stopped when the button is clicked. Please help me out.

Whenever i click the button, the app crashes. I want the app to open the url in a webview when the button is clicked. Please help me out, any help is greatly appreciated.

Can anyone please post the modified code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >



<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:text="@string/button01" 
        android:onClick="onClick"/>
<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:text="@string/button02" 
        android:onClick="onClick"/>


</LinearLayout>   
------------------------------
screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<WebView 
    android:id="@+id/webview" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>
-----------------------------------------------------
mainactivity.java

package com.example.webview;



import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends ActionBarActivity {


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


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick(View v)
    {
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        final Context context = this;
        Intent intent = new Intent(context, SecondActivity.class);
        startActivity(intent);
       switch(v.getId())
       {
           case R.id.button1:
               myWebView.loadUrl("http://techcrunch.com/tag/rss/");
                                break;

          case R.id.button2: 
              myWebView.loadUrl("http://www.bestchance.org.au/");
                               break;

          default:
                               break;
      }


}
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
--------------------
secondactivity.java
package com.example.webview;

import android.webkit.WebView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class SecondActivity extends ActionBarActivity {

    private WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen3);




 }


}
-------------------------------

Solution

  • The reason why it's crashing is because you are calling the webview which belongs to another activity. So instead, just pass a string from your activity to second activity then get the string and load it to your webview. Below is how you can achieve it:

    On the first activity, edit your onClick method to:

    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
    
        switch(v.getId())
        {
            case R.id.action_bar:
                intent.putExtra("url", "http://techcrunch.com/tag/rss/");
                startActivity(intent);
                break;
    
            case R.id.action_bar_spinner:
                intent.putExtra("url", "http://www.bestchance.org.au/");
                startActivity(intent);
                break;
    
            default:
                break;
        }
    }
    

    then in your SecondActivity:

    public class SecondActivity extends ActionBarActivity {
        private WebView myWebView;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen3);
    
        String url = getIntent().getStringExtra("url");
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl(url);
     }
    }