Reputation: 31
I am trying to load webpages in fragments to use in my new app, the problem is the code runs but for every website it shows me a white screen, no crashing or error messages. I am also trying to do this in Kotlinwhich might not be a good idea...But currently still have the time to port to java AndroidManiferst.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.braun.testingwebview">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestingWebView">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.TestingWebView.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".FirstFragment">
<TextView
android:id="@+id/textView"
android:layout_width="213dp"
android:layout_height="108dp"
android:text="Test"
tools:ignore="MissingConstraints" />
<WebView
android:id="@+id/WebView1"
android:layout_width="match_parent"
android:layout_height="600dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</LinearLayout>
FirstFragment.kt
package com.braun.testingwebview
import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.fragment_first.*
class FirstFragment : Fragment() {
@SuppressLint("SetJavaScriptEnabled")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val myWebView: WebView = WebView1
myWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView,
url: String
): Boolean {
view.loadUrl(url)
return true
}
}
myWebView.loadUrl("https://google.com")
myWebView.settings.javaScriptEnabled = true
myWebView.settings.allowContentAccess = true
myWebView.settings.domStorageEnabled = true
myWebView.settings.useWideViewPort = true
return inflater.inflate(R.layout.fragment_first, container, false)
}
}
Thanks for any help in advance!
Upvotes: 3
Views: 6300
Reputation: 11
I probably had the same issue, ending in a white screen. Figured out I can just call loadurl on my WebView, without using a WebViewClient and it works. Not sure if it's good practice, but atleast it did display some content
class WebViewFragment : Fragment() {
private var _binding: FragmentWebViewBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentWebViewBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val myWebView = binding.wvWebViewScreen
myWebView.loadUrl(url)
}
}
web_view.xml is just a simple webview wrapped by Relative Layout.
Upvotes: 0
Reputation: 43
This worked for me
class LogoutFragment : Fragment() {
private var _binding: FragmentLogoutBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentLogoutBinding.inflate(inflater, container, false)
val root: View = binding.root
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val myWebView: WebView = view.findViewById(R.id.WebView)
myWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView,
url: String
): Boolean {
view.loadUrl(url)
return true
}
}
val webSetting:WebSettings = myWebView.settings
webSetting.javaScriptEnabled = true
myWebView.webViewClient = WebViewClient()
myWebView.canGoBack()
myWebView.setOnKeyListener(View.OnKeyListener { v , keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.action == MotionEvent.ACTION_UP
&& myWebView.canGoBack()){
myWebView.goBack()
return@OnKeyListener true
}
false
})
myWebView.loadUrl("https://youtube.com")
myWebView.settings.javaScriptEnabled = true
myWebView.settings.allowContentAccess = true
myWebView.settings.domStorageEnabled = true
myWebView.settings.useWideViewPort = true
}
}
Upvotes: 0
Reputation: 3622
Put WebView part of code in onViewCreated rather than of onCreateView, in onCreateView just inflate the fragment'x xml layout and return the view. Action on fragment container should be made in onViewCreated after view created by onCreateView function.
So your modified code will be:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?){
val myWebView: WebView = view.findViewById(R.id.WebView1)
myWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView,
url: String
): Boolean {
view.loadUrl(url)
return true
}
}
myWebView.loadUrl("https://google.com")
myWebView.settings.javaScriptEnabled = true
myWebView.settings.allowContentAccess = true
myWebView.settings.domStorageEnabled = true
myWebView.settings.useWideViewPort = true
}
Upvotes: 3