I have to load HTML content in webview inside nested scrollview but wehn i load content some of the content is disappear and display white space.
When i long click on any part of that white screen and copy content is copied in clipboard.
I checked with removing nested scroll view it will working as expected.
Below is my XML code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=";
xmlns:app=";
xmlns:tools=";
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- NestedScrollView to enable scrolling -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<!-- LinearLayout that holds content, including WebView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Some content above WebView (e.g., header, image, etc.) -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#5C719B" />
<!-- WebView without ScrollView, but inside the NestedScrollView -->
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:scrollbars="none"
android:minHeight="200dp"
android:background="@android:color/transparent" />
<!-- Content below WebView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#5C719B" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Below is my Java code
package com.tops.webviewgs
import android.R
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.webkit.ConsoleMessage
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebView.setWebContentsDebuggingEnabled
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.NestedScrollView
import com.tops.webviewgs.databinding.ActivityMainBinding
import java.io.BufferedReader
import java.io.InputStreamReader
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val htmlContent = readHtmlFromAssets(this, "news.html")
loadDetailsDataIntoWebview(binding.webview, htmlContent, 0)
}
fun readHtmlFromAssets(context: Context, fileName: String): String {
return try {
val inputStream = context.assets.open(fileName)
val reader = BufferedReader(InputStreamReader(inputStream))
val stringBuilder = StringBuilder()
var line: String?
while (reader.readLine().also { line = it } != null) {
stringBuilder.append(line).append("\n")
}
reader.close()
stringBuilder.toString()
} catch (e: Exception) {
e.printStackTrace()
"Error reading file"
}
}
private fun loadDetailsDataIntoWebview(
wvNewsDetail: WebView?,
articleContent: String,
from: Int
) {
wvNewsDetail?.apply {
// WebView settings
// clearHistory()
// clearCache(true)
settings.apply {
javaScriptEnabled = true
domStorageEnabled = true
defaultFontSize = 20
loadWithOverviewMode = true
useWideViewPort = false
databaseEnabled = true
loadsImagesAutomatically = true
cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
mediaPlaybackRequiresUserGesture = true
layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
offscreenPreRaster = true
allowFileAccess = true
allowContentAccess = true
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mixedContentMode = 0;
setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
// Prevent WebView from being reset
isVerticalScrollBarEnabled = true
overScrollMode = WebView.OVER_SCROLL_NEVER
scrollBarStyle = View.SCROLLBARS_INSIDE_INSET
webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
val blockedUrls = listOf(
"ads.js",
"tracking.js",
"some-script.js"
) // Add problematic scripts here
if (request?.url.toString().contains(blockedUrls.toString())) {
Log.e("WEBVIEW", "Blocked script: ${request?.url}")
return WebResourceResponse("text/plain", "utf-8", null) // Block the script
}
return super.shouldInterceptRequest(view, request)
}
@Deprecated("Deprecated in Java")
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
return false
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
}
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
}
}
webChromeClient = object : WebChromeClient() {
override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
consoleMessage?.message()?.let {
Log.e("JS_ERROR", it)
}
return true
}
}
setOnKeyListener(object : View.OnKeyListener {
override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean {
if (event.action != KeyEvent.ACTION_DOWN) {
when (event.keyCode) {
KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN -> // Handle volume key events as needed
return true // Consume the event
}
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (canGoBack()) {
goBack()
return true
} else {
Log.e(
"WEBVIEW",
"BACK PRESSED")
return true
}
}
return false
}
})
setWebContentsDebuggingEnabled(true)
loadDataWithBaseURL("about:blank", articleContent, "text/html", "UTF-8", null)
// loadUrl("file:///android_asset/news.html")
}
}
}
Here is output which i got from above code