Question

The following code is working but I want the links to open in a webview. How do I do that? I have created a webview activity called WebbVy.java but I'm guessing that I need to create an intent after the textview below that starts the webview activity. But how do I do that? At the moment, the textview is populated by title, pubdate, description and link (opens in browser) and I need the link to open in a webview instead of the browser.

VisaHelaNyheten.java

package se.sebastianliljegren.rss_feed;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class VisaHelaNyheten extends Activity{
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.visa_hela_nyhet);

    String enNyhet = null;
    String mLink = null;


    final Intent bundleintent = getIntent();

    if (bundleintent != null)
    {
        Bundle b = bundleintent.getBundleExtra("BUNDLE.VISA.SEN.INTENT");
        if (b == null)
        {
            enNyhet = "fel bundle?";
        }
        else
        {
            enNyhet = b.getString("title") + "\n\n" + b.getString("pubdate") + "\n\n" + b.getString("description") + "\n" + "\n\nMore information:\n" + b.getString("link");
            mLink = b.getString("link");

        }
    }
    else
    {
        enNyhet = "Information ej hittad.";
    }

    TextView db = (TextView) findViewById(R.id.nyhet);
    db.setText(enNyhet);
    TextView link = (TextView) findViewById(R.id.link);
    link.setText(mLink);

    link.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(VisaHelaNyheten.this, WebbVy.class);
            intent.putExtra("LINK" mLink);
            startActivity(intent);
        }
    });
        }
    }

WebbVy.java package se.sebastianliljegren.rss_feed;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("SetJavaScriptEnabled")
public class WebbVy extends Activity {

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


    Intent intent = getIntent();
    String link = intent.getStringExtra("link");

    WebView mainWebView = (WebView) findViewById(R.id.webview1);

    WebSettings webSettings = mainWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mainWebView.setWebViewClient(new MyCustomWebViewClient());
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    mainWebView.loadData(link, "text/html; charset=utf-8","utf-8");

}

private class MyCustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}

webview.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webview1" >

</WebView>
Was it helpful?

Solution

Use a separate TextView to store the link like:

String enNyhet = null;
String mLink = null

enNyhet = b.getString("title") + "\n\n" + b.getString("pubdate") + "\n\n" + b.getString("description") + "\n";
mLink = b.getString("link");

TextView db = (TextView) findViewById(R.id.nyhet);
db.setText(enNyhet);
TextView link = (TextView) findViewById(R.id.link);
link.setText(mLink);

Now add a onClickListener to the link TextView:

link.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Set the intent to your webview activity and put the string link as extra.
            Intent intent = new Intent(this, WebbVy.class);
            intent.putExtra("LINK", mLink);
            startActivity(intent);                
        }
    });

Then in your webview activity you get the string from the intent and load it in the webview.

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top