user1184205
user1184205

Reputation: 863

Can't access EditText or other UI components with Kotlin

I'm using Android Studio 3.0 RC2 & Kotlin.

When I try to access a UI component the app crashes unless I first write findViewById. I thought Kotlin was supposed to get rid of having to write findViewById line? The UI is a fragment and I'm trying to access from the same fragment code. Is there a way to not have to write findViewById?

These lines work:

var userNameField = view?.findViewById<EditText>(R.id.userNameTextField) as EditText
userNameField.setText("hello world")

This line doesn't work without findViewById line

userNameTextField.setText("hello world")

I even have

import kotlinx.android.synthetic.main.fragment_sign_in.*

The onCreateView() code:

 override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    var view = inflater!!.inflate(R.layout.fragment_sign_in, container, false)

    var userNameField = view?.findViewById<EditText>(R.id.userNameTextField) as EditText
    userNameField.setText("hello world")

    return view
}

Upvotes: 7

Views: 2616

Answers (5)

moonkin
moonkin

Reputation: 493

For those who have this problem in 2021:

I know this question is asked in the past but since it is still relevant here I want to help others: today I ran to this problem and tried the suggested ways and unfortunately, the

plugins { ... id 'kotlin-android-extensions' }

is not working anymore or more accurately is deprecated, so try to use the Jetpack view binding approach and you'll be good.

The message I got after adding the Gradle Kotlin Android extension dependency is: The 'kotlin-android-extensions' Gradle plugin is deprecated. Please use this migration guide (click here) to start working with View Binding. (click here) and the 'kotlin-parcelize' plugin.

Upvotes: 0

user14704851
user14704851

Reputation: 21

Was unable to access the ui components like OP faced looks like below was needed inside app gradle:

plugins {
    ...
    id 'kotlin-android-extensions'
}

Even though after adding this line android studio was still not able to auto resolve the imports for kotlin synthetics so you may need to invalidate cache and restart.

If still not working then import manually depending on views

  • activity /fragment view: import kotlinx.android.synthetic.main.<your_activity_view>.*
  • normal views : import kotlinx.android.synthetic.main.<your_layout_view>.view.*

Upvotes: 2

Dirk H&#246;nisch
Dirk H&#246;nisch

Reputation: 67

I was brought here by searching for the same issue. I missed to add the kotlin-android-extensions to the build.gradle:

apply plugin: 'kotlin-android-extensions'

Upvotes: 3

Kushal Desai
Kushal Desai

Reputation: 127

I faced a similar problem.

I had a private function. I'd also imported the kotlinx library. But I was not able to access the editText from that function.

I just removed the function and defined it again.

Voila! It worked.

Upvotes: 0

Fredy Mederos
Fredy Mederos

Reputation: 2636

In the onCreateView just return the inflated view.

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_sign_in, container, false)

}

In the onViewCreated you can access your view components

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        userNameField.setText("hello world")
    }

Upvotes: 3

Related Questions