请查看此博客了解更多详情https://medium.com/@sajaljain98/embedded-tweets-in-android-webview-1363012746e3
private fun handleInWebiew() {
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 loaded
val 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() {
override fun onPageFinished(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)
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
}
override fun shouldOverrideUrlLoading(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)))
}
}
return true
}
}
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
}}