How To Correctly Embed Tweet Inside Android Webview?
I have an issue with a WebView containing some HTML data (I don't load an URL but custom content in HTML). I use: mWebView.loadData(mContent, 'text/html; charset=utf-8', 'utf-8');
Solution 1:
There are a couple of ways I can think of to solve this issue:
You could replace your call to loadData
with:
loadDataWithBaseURL("https://twitter.com", mContent, "text/html", "UTF-8", null);
Or, you could make sure that the html string (mContent) has the following in its head:
<scripttype="text/javascript"src="https://platform.twitter.com/widgets.js"></script>
If you don't have control of the incoming HTML, you could try using Jsoup to amend the content yourself.
Documentdoc= Jsoup.parse(mContent);
doc.head().appendElement("script").attr("type", "text/javascript").attr("src", "https://platform.twitter.com/widgets.js");
// This is the html that includes the appropriate reference to Twitter's widgets.js scriptStringnewHtml= doc.toString();
Solution 2:
Call loadDataWithBaseURL instead of loadData, but you don't have to use https://twitter.com. Just use your websites protocol and domain, like this:
loadDataWithBaseURL("https://mywebsite.com", mContent, "text/html", "UTF-8", null);
Solution 3:
please check out this blog for more details https://medium.com/@sajaljain98/embedded-tweets-in-android-webview-1363012746e3
privatefunhandleInWebiew() {
val value : String = "<blockquote class="twitter-tweet"><p lang="hi" dir="ltr">T 3644 -<br>छू लो तो चरण<br>अड़ा दो तो टांग<br>धंस जाए तो पैर<br>फिसल जाए तो पाँव<br>आगे बढ़ना हो तो कदम<br>राह में चिह्न छोड़े तो पद<br>प्रभु के हों तो पदुका *<br>गधे की पड़े तो *दुलत्ती<br>घुंघरु बांधो तो पग<br>खाने के लिए टंगड़ी<br>खेलने के लिए लंगड़ी<br><br>अंग्रेज़ी में only , LEG</p>— Amitabh Bachchan (@SrBachchan) <a href="https://twitter.com/SrBachchan/status/1307062341390020609?ref_src=twsrc%5Etfw">September 18, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>"val webView = findViewById<>(R.id.webView)
webView.visibility = View.INVISIBLE //so that we can show progress bar util every thing is loadedval progressBar = findViewById<>(R.id.progress_loader)
if (!TextUtils.isEmpty(value)) {
Log.d(TAG, "bindData: ")
val webSettings = webView.settings
webView.setWebChromeClient(WebChromeClient())
webSettings.javaScriptEnabled = true
webSettings.domStorageEnabled = true
webSettings.loadsImagesAutomatically = true
webSettings.defaultTextEncodingName = "UTF-8"
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL)
webSettings.setUseWideViewPort(false)
webView.setHorizontalScrollBarEnabled(false);
webView.setVerticalScrollBarEnabled(false);
webView.setScrollContainer(false);
webView.setOnTouchListener { v: View?, event: MotionEvent -> event.action == MotionEvent.ACTION_MOVE }
webView.webViewClient = object : WebViewClient() {
overridefunonPageFinished(view: WebView?, url: String?) {
Log.d(TAG, "onPageFinished: url $url")
//There is some delay in loading block-quote so avoid showing html without //loading js putting this fake delay
Handler().postDelayed({
if (progressBar != null) {
progressBar.visibility = View.GONE
}
webView.visibility = View.VISIBLE
}, 3000)
}
overridefunonPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
}
overridefunshouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
Log.d(TAG, "shouldOverrideUrlLoading: url " + url)
if (!TextUtils.isEmpty(url)) {
val uri = Uri.parse(url)
val twitter = Intent(Intent.ACTION_VIEW, uri)
twitter.setPackage("com.twitter.android")
try {
mContext.startActivity(twitter)
} catch (e: ActivityNotFoundException) {
mContext.startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse(url)))
}
}
returntrue
}
}
try {
webView.loadDataWithBaseURL("https://twitter.com", value, "text/html", "utf-8", null)
} catch (e: Exception) {
e.printStackTrace()
}
} else {
if (progressBar != null)
progressBar.visibility = View.GONE
}}
Post a Comment for "How To Correctly Embed Tweet Inside Android Webview?"