0

How can I display only this part of webpage in my webview I tried many things but its not working out

enter image description here

class ActionFragment : Fragment() {
    class MyWebClient : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            view.loadUrl(url)
            return true
        }

        override fun onPageFinished(view: WebView, url: String) {
            view.loadUrl("javascript:document.getElementByClassName('fc-main-container')")

        }
    }
    private var _binding: FragmentActionBinding? = null
        private val binding get() = _binding!!

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(R.layout.fragment_action, container, false)

        val mWebView = view.findViewById(R.id.webpage1) as WebView
        mWebView.loadUrl("https://www.webmd.com/diet/healthtool-food-calorie-counter")

        val webSettings = mWebView.settings
        webSettings.javaScriptEnabled = true
        mWebView.webViewClient = MyWebClient()
        return view
    }
}

Can anyone help me out,Im very beginner in android development and have no knowledge of javaScript.I just need it for my college project.

PCAPS
  • 39
  • 4
  • Does this answer your question? [Android Calling JavaScript functions in WebView](https://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview) – user2357113 Mar 03 '23 at 08:51
  • @mujammil no I am not getting it, sorry I tried using function / * override fun onPageFinished(view: WebView, url: String) { view.loadUrl("javascript:(function() { " + "document.getElementsByClassName('fc-main-container').style.display='none';"+"} )()") } */ still not working – PCAPS Mar 03 '23 at 09:01

1 Answers1

1

My workable code, please check.

private var currentUrl: String = "www.example.com/test"
 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
        binding.webviewService.isVerticalScrollBarEnabled = true;
        binding.webviewService.loadUrl(currentUrl)
        val policy = ThreadPolicy.Builder().permitAll().build()
        StrictMode.setThreadPolicy(policy)
        val document: Document = Jsoup.connect(currentUrl).get()
        document.getElementsByClass("header-area").remove()
        document.getElementsByClass("breadcrumb-area").remove()
        document.getElementsByClass("footer-area").remove()
        val settings: WebSettings = binding.webviewService.settings
        settings.allowUniversalAccessFromFileURLs = true
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.cacheMode = WebSettings.LOAD_NO_CACHE
        settings.setAppCacheEnabled(false)
        settings.domStorageEnabled = true

        binding.webviewService.loadDataWithBaseURL(
            currentUrl,
            document.toString(),
            "text/html",
            "utf-8",
            ""
        );


    }

Here "webview_service" is referecnce from layout "WebView" Tag. You need to pass your actual url in "currentUrl" variable. You also need to add jsoup library to dependencies in build gradle file.

implementation 'org.jsoup:jsoup:1.11.1'

Here you have to know the html tag reference id from web which would be hidden. In above-mentioned code, below part is used to hide specific part of webview.

val document: Document = Jsoup.connect(currentUrl).get()
        document.getElementsByClass("header-area").remove()
        document.getElementsByClass("breadcrumb-area").remove()
        document.getElementsByClass("footer-area").remove()
Tausif
  • 778
  • 1
  • 5
  • 14