androidhtmlwebview

Why WebView doesn't render raw HTML properly?


Maybe my question is a kind of stupid question but webview doesn't interpret html tags. I searched but didn't find any problem like this. Please let me know if you have any idea.

This is my code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Get context of application
        mContext = getActivity().getApplicationContext();

        // Assign layout to fragment
        View view = inflater.inflate(R.layout.dialog_product_info, container, false);

        mWebView = (WebView) view.findViewById(R.id.webView);
        mWebView.setBackgroundColor(getResources().getColor(R.color.transparent));

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getDialog().setTitle(mProduct.getName());

        mWebView.loadDataWithBaseURL(null, "<HTML>" + mProduct.getDescription() + "</HTML>", "text/html", "utf-8", null);

    }

Screenshot:

enter image description here


Solution

  • Based on this answer, I downloaded commons-lang3-3.3.2-bin.zip file added to my lib folder and changed my code to the following. It's working fine now.

    @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            getDialog().setTitle(mProduct.getName());
    
            String description = mProduct.getDescription();
            description = StringEscapeUtils.unescapeHtml4(description);
            mWebView.loadDataWithBaseURL(null, "<HTML>" + description + "</HTML>", "text/html", "utf-8", null);
        }