Reputation: 3964
when I try to below code webview.heigth
returns 0. I don't understand whats wrong with the code. I also tried the measuredheight
instead of height but the result is still same.
if (!TextUtils.isEmpty(realtyDetail.descriptionHtml)) {
webViewHtml.loadData(FormatUtil.formatSpecialCharsForWebView(realtyDetail.descriptionHtml), "text/html; charset=utf-8", "UTF-8")
ellWebView.visible()
txtDescription.visible()
webViewHtml.settings.javaScriptEnabled = true
val vto = view!!.viewTreeObserver;
vto.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (vto.isAlive) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
vto.removeOnGlobalLayoutListener(this)
} else {
@Suppress("DEPRECATION")
vto.removeGlobalOnLayoutListener(this)
}
if (!webViewHtmlSet && webViewHtml != null) {
val limit = DeviceUtil.convertDpToPixel(400f, context)
if (webViewHtml.height > limit) {
setWebView(limit)
}
}
}
}
})
layout:
<WebView
android:id="@+id/webViewHtml"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
html:(my webview height is wrap_content
but only below html not working)
<font style="font-size:22px"><font style="background-color:#00ff00"><font style="color:#000000"><i>KOCAELİ KARAMÜRSEL AVCİ KOYDE KÖY CIVARI MEVKİNDE 723M2 MUSTEKİL PARSEL SATILIK TIR <br/></i></font></font></font><font style="font-size:22px"><font style="background-color:#00ff00"><font style="color:#000000"><i><br/></i></font></font></font><font style="font-size:22px"><font style="background-color:#00ff00"><font style="color:#000000"><i>PARKE YOLA CEPHE LI DÜMDÜZ %30 KONUT IMARLI ARSA,DIR DÜMDÜZ CEVİZ BAHÇESİ ELEKTRİK HATTI ŞEBEKE SU HATTI YANINDAN GEÇMEKTEDİR KANALIZASYON TÜM ALT YAPILARI TAMAMLANMIŞ TIR TOPLU ULAŞIM VARDIR ARADIĞINIZ HER ŞEY YÜRÜ ME MESAFESINDE DİR CAMI KAHVANE BERBER NALBUR PETROL OFİSİ SAĞLIK OCAĞI İTFAİYE ILKÖĞRETİM <br/></i></font></font></font><font style="font-size:22px"><font style="background-color:#00ff00"><font style="color:#000000"><i><br/></i></font></font></font><font style="font-size:22px"><font style="background-color:#00ff00"><font style="color:#000000"><i>Not istek halinda yan KOMŞU parselde SATILIK 1013m2<br/></i></font></font></font><font style="font-size:22px"><font style="background-color:#00ff00"><font style="color:#000000"><i><br/></i></font></font></font><font style="font-size:22px"><font style="background-color:#00ff00"><font style="color:#ff0000"><i>DAHA DETAYLI BILGI ALMAK İÇİN BIZI ARAYINIZ </i></font></font></font>
by the way this is working on simulator but on real device the html doesn't work and the height returns 0.
Upvotes: 1
Views: 1088
Reputation: 6277
Attach viewTreeObserver
to the WebView
on onPageFinished()
callback on the WebViewClient
like below. it gives the exact height of your webView.
webView.webViewClient = Client()
webView.loadData(Base64.encodeToString("<font style=\"font-size:22px\"><font style=\"background-color:#00ff00\"><font style=\"color:#000000\"><i>KOCAELİ KARAMÜRSEL AVCİ KOYDE KÖY CIVARI MEVKİNDE 723M2 MUSTEKİL PARSEL SATILIK TIR <br/></i></font></font></font><font style=\"font-size:22px\"><font style=\"background-color:#00ff00\"><font style=\"color:#000000\"><i><br/></i></font></font></font><font style=\"font-size:22px\"><font style=\"background-color:#00ff00\"><font style=\"color:#000000\"><i>PARKE YOLA CEPHE LI DÜMDÜZ %30 KONUT IMARLI ARSA,DIR DÜMDÜZ CEVİZ BAHÇESİ ELEKTRİK HATTI ŞEBEKE SU HATTI YANINDAN GEÇMEKTEDİR KANALIZASYON TÜM ALT YAPILARI TAMAMLANMIŞ TIR TOPLU ULAŞIM VARDIR ARADIĞINIZ HER ŞEY YÜRÜ ME MESAFESINDE DİR CAMI KAHVANE BERBER NALBUR PETROL OFİSİ SAĞLIK OCAĞI İTFAİYE ILKÖĞRETİM <br/></i></font></font></font><font style=\"font-size:22px\"><font style=\"background-color:#00ff00\"><font style=\"color:#000000\"><i><br/></i></font></font></font><font style=\"font-size:22px\"><font style=\"background-color:#00ff00\"><font style=\"color:#000000\"><i>Not istek halinda yan KOMŞU parselde SATILIK 1013m2<br/></i></font></font></font><font style=\"font-size:22px\"><font style=\"background-color:#00ff00\"><font style=\"color:#000000\"><i><br/></i></font></font></font><font style=\"font-size:22px\"><font style=\"background-color:#00ff00\"><font style=\"color:#ff0000\"><i>DAHA DETAYLI BILGI ALMAK İÇİN BIZI ARAYINIZ </i></font></font></font>".toByteArray(), Base64.NO_PADDING), "text/html", "base64")
....
private inner class Client: WebViewClient(){
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
webView.viewTreeObserver.addOnGlobalLayoutListener {
Log.d("xifi", "${webView.height}")
}
}
}
This is the output I got for your HTML
This is emulator displaying webPage
Upvotes: 1