1

While I was searching for how to fire my own activity for links in html parsed with Jsoup, I stumbled upon LinkEnabledTextView In the demo, it was implemented for strings but in my case, I am fetching the text from the web and displaying it in a TextView.

In the accepted answer in this question, it was applied to listitem. I followed the instructions in both links but I am having problems applying it to my scenario.

NewsDetails class

public class NewsDetails extends AppCompatActivity implements TextLinkClickListener{


    TextView newsTitle;
    LinkEnabledTextView newsContent;

    private String news_content;


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

        showDialog();

        newsTitle = (TextView) findViewById(R.id.dnews_title);
        newsAuthorDate = (TextView) findViewById(R.id.author_date);
        newsContent = (LinkEnabledTextView) findViewById(R.id.dnews_content);


        check = new LinkEnabledTextView(this, null);
        check.setOnTextLinkListener(this);
        check.gatherLinksForText(String.valueOf(newsContent));
        check.setTextColor(Color.BLUE);
        check.setLinkTextColor(Color.RED);


        MovementMethod movementMethod = check.getMovementMethod();
        if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod)) {
            if (check.getLinksClickable()) {
                check.setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
       //Skipped unecassry codes
    }

    private void showDialog() {
        internetDialog = new AlertDialog.Builder(NewsDetails.this)
                ...
    }

    private void loadNews() {
        Log.d(TAG, "loadNews called");

        final ProgressBar progressBar;
        progressBar = (ProgressBar) findViewById(R.id.progress_circle);
        progressBar.setVisibility(View.VISIBLE);


        String news_id = getIntent().getStringExtra("NewsId");
        Log.d(TAG, "You clicked news id " + news_id);

        StringRequest stringRequest = new StringRequest(news_id,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Log.d("Debug", response.toString());
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }
                        parseHtml(response);
                        newsData = response;
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("", "Error: " + error.getMessage());

                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }

                        final  AlertDialog.Builder sthWrongAlert = new AlertDialog.Builder(NewsDetails.this);
                        ...
                        sthWrongAlert.show();
                    }
                });

        //Creating requestqueue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request queue
        requestQueue.add(stringRequest);

    }

    private void parseHtml(String response) {
        Log.d(TAG, "parsinghtml");
        Document document = Jsoup.parse(response);
        String news_title = document.select("h1.entry-title").first().text();
        news_content = document.select("div.entry-content").first().html();

        newsTitle.setText(news_title);
       Spanned spanned = Html.fromHtml(news_content, new UILImageGetter(newsContent, this), null );
        newsContent.setText(spanned);

        //Unhiding views
        newsTitle.setVisibility(View.VISIBLE);
        newsContent.setVisibility(View.VISIBLE); 
    }


}

I have tried to pass String.valueOf(newsContent) to check.gatherLinksForText but it didn't work. I have also tried check.gatherLinksForText(news_content) but it throws NPE.

Please what is the right parameter that I should pass check.gatherLinksForText() to make it work?

Community
  • 1
  • 1
X09
  • 3,827
  • 10
  • 47
  • 92

0 Answers0