How do I add a webView into a fragment view android Studio

Viewed 16

I have an app which has a MainActivity, ClientFragment,and ConnectFragment. I want to add a webView to the ClientFragment. However when I try to add it I get an error unresolved reference: findViewById.
I have added to my MainActivity but then the rest of my app doesn't work right. How and where should I add my WebView?

package com.example.mqttkotlinsample

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.ImageButton
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import org.eclipse.paho.client.mqttv3.*

class ClientFragment : Fragment() {
private lateinit var mqttClient : MQTTClient

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            if (mqttClient.isConnected()) {
                // Disconnect from MQTT Broker
                mqttClient.disconnect(object : IMqttActionListener {
                    override fun onSuccess(asyncActionToken: IMqttToken?) {
                        Log.d(this.javaClass.name, "Disconnected")

                        Toast.makeText(context, "MQTT Disconnection success", Toast.LENGTH_SHORT).show()

                        // Disconnection success, come back to Connect Fragment
                        findNavController().navigate(R.id.action_ClientFragment_to_ConnectFragment)
                    }

                    override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
                        Log.d(this.javaClass.name, "Failed to disconnect")
                    }
                })
            } else {
                Log.d(this.javaClass.name, "Impossible to disconnect, no server connected")
            }
        }
    })
}

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?


): View? {
// Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_client, container, false)

//I have tried this many places but with no luck same error unresolved reference:findViewById.

    val webView = findViewById<View>(R.id.webView) as WebView





}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}}

I have place the WebView in the xml file.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout       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:background="@drawable/mainwater"

tools:context=".ConnectFragment">


<WebView
    android:id="@+id/webView"
    android:layout_width="432dp"

    android:layout_height="225dp"
    android:layout_marginBottom="224dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView6" />

If anyone can help me get this working I would really appreciate it.

1 Answers

I got it figured out all I had to do was put val webView = view.findViewById<View>(R.id.webView) as WebView

Related