Norbert Maszlag
Norbert Maszlag

Reputation: 11

How do can I access UI elements from a Fragment in Android?

I'm building a simple Android application for a university project and ran into some problems using fragments. I have no idea how to access UI element inside a fragment.

There are some example projects available to us that should answer questions like this, but honestly this particular code made me even more confused.

In the example code below the ID alone should enough to refer to a TextView from the XML file that is created for this particular fragment, but in reality these 4 lines glow up like Christmas lights in the IDE and I can not seem to figure out how to resolve the issue.

What should I do to make this code work? What are other alternative ways to access TextViews (and other UI elements) inside a fragment using Kotlin?

class DetailsProfileFragment : Fragment(R.layout.profile_detail){

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

        val person = DataManager.person

        tvId.text = person.id
        tvSSN.text = person.socialSecurityNumber
        tvTaxId.text = person.taxId
        tvRegistrationId.text = person.registrationId

    }
}

Upvotes: 0

Views: 1261

Answers (1)

lucster
lucster

Reputation: 179

You should change:

tvId.text = person.Id

And the other lines into:

view.findViewById<TextView>(R.id.tvId).text = person.Id

Upvotes: 1

Related Questions