srini vasan
srini vasan

Reputation: 11

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);




 }


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

Upvotes: 1

Views: 16266

Answers (4)

Mayank Ingle
Mayank Ingle

Reputation: 1

add Internet permission in your Manifest

<uses-permission android:name="android.permission.INTERNET"/>

activity_main.xml

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

    <WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/webview">
    </WebView>
    <androidx.appcompat.widget.AppCompatButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:id="@+id/btn_start"
        android:textSize="20sp"
        android:background="@android:color/darker_gray"
        android:layout_marginTop="250dp"
        android:layout_gravity="center"
        android:textColor="@color/white"
        android:padding="30dp">
    </androidx.appcompat.widget.AppCompatButton>

</LinearLayout>



MainActivity.kt

package com.example.webview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn_start.setOnClickListener { web_view() }

    }

private fun web_view(){ webview.webViewClient = WebViewClient()

    // this will load the url of the website
    webview.loadUrl("your url")

    // this will enable the javascript settings
    webview.settings.javaScriptEnabled = true

    // if you want to enable zoom feature
    webview.settings.setSupportZoom(true) 
}

    // if you press Back button this code will work
    override fun onBackPressed() {
        // if your webview can go back it will go back
        if (webview.canGoBack())
            webview.goBack()
        // if your webview cannot go back
        // it will exit the application
        else
            super.onBackPressed()
    }
}

Upvotes: 0

Genevieve
Genevieve

Reputation: 450

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);
 }
}

Upvotes: 7

BSathvik
BSathvik

Reputation: 95

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);
    String url;

   switch(v.getId())
   {
       case R.id.button1:
           url ="http://techcrunch.com/tag/rss/";
                            break;

      case R.id.button2: 
          url = "http://www.bestchance.org.au/";
                           break;

      default:
                           break;
  }
startActivity(intent);
myWebView.loadUrl(url);
}

Just remove your code and paste this in.. you were trying to access the buttons that werent there because your started a new activity where the layout changed and there werent button to call them..

Upvotes: 0

Tomer Shemesh
Tomer Shemesh

Reputation: 13445

you might need to post you code here to see whats going on, but if your just trying to get a a webview popup you could just launch a browser activity to load that page

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

if you need it to be your activity you will need to pass an intent to your new activity and get the url out and then when you will just load it

webView.loadUrl(URL);

if the issue is the crashing post the crashlog here so we can take a look what is going wrong

Upvotes: 0

Related Questions