<Button
android:id="@ id/createBirthdayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:padding="20dp"
android:text="@string/press_to_create"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/Sample_message_1" />
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createBirthdayButton // This is not working
}
Here id name is "createBirthdayButton" but i am not able to access it in my Main Activity class
CodePudding user response:
Please use like this :
class MainActivity : AppCompatActivity() {
lateinit var button : Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button = findViewById(R.id.yourButtonId)
}
}
CodePudding user response:
unless you are using view binding
(i can't see that in your code and also you are using setContentView(R.layout.activity_main)
),
you need to cast your view first like the top comments:
Button createBirthdayButton =findViewById(R.id.createBirthdayButton)
and if in the future you decided to go with view binding, it will be like this:
binding.createBirthdayButton