My material components are not being recognized in my Activity

Asked

Viewed 70 times

0

Reinstalled Android Studio on my computer, went to make a basic application Only login screen and a button to advance But in Activity, when typing the id that was assigned in xml, the Kotlin file does not recognize anything that is there. Neither button nor text.

Kotlin archive


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnAvancar.setOnClickListener(){

        }
    }
}```

**Arquivo XML** 

```<?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:id="@+id/Tela_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/User"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/user_name" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/Senha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/User">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/btnAvancar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="30dp"
        android:layout_marginRight="30dp"
        android:clickable="true"
        app:backgroundTint="@color/purple_500"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Senha"
        app:srcCompat="@android:drawable/ic_media_play" />

</androidx.constraintlayout.widget.ConstraintLayout>'''
  • Maybe it’s missing: apply plugin: ?Kotlin-android-Extensions'. However this plugin has been deprecated for Kotlin 1.4, https://youtrack.jetbrains.com/issue/KT-42121.

1 answer

-1

You just need to add the following line in your method onCreate():

val bt = findViewById<Button>(R.id.btnAvancar)

So you do a search in your xml for the button identifier and can access it from the variable bt. Your code will look like this:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        val bt = findViewById<Button>(R.id.btnAvancar)

        bt.setOnClickListener(){
            //sua ação de clique aqui
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.