Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to change the text value in a textview, whilst in a fragment. Kotlin

Does anyone know of a way to change the value of a textview text, whilst in a fragment. I have tried findviewbyid, however i believe that this does not work whilst in a fragement. Does anyone have an idea on how to identify a textview and change the text value of it. Thanks


class Home : Fragment() {

    override fun onCreateView(

        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view =  inflater.inflate(R.layout.fragment_home, container, false)

        val queue = Volley.newRequestQueue(view.context)
        val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" //URL
        var urlStuff = ""

        val stringRequest = StringRequest(Request.Method.GET, url,
            { response ->
                // Display the first 500 characters of the response string.
                urlStuff = response
                val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
                val id = jsonArray.getJSONObject(0).getString("value") // Get data
                Log.i("Val: ", id)

                //Updating info on app
                val textView = view.findViewById(R.id.temp)                 
                textView.text = "My Text"


            },
            { Log.i("b", "That didn't work!") })
        queue.add(stringRequest)
        return view

    }


}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

findViewById also works in a fragment, like this:

val view =  inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = view.findViewById(R.id.temp)
textView.text = "My Text"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading