DATA DATA
DATA DATA

Reputation: 73

Kotlin Not Recognizing ID of Android XML Elements

Hello fellow programmers!

i try to call element from my xml file example : a " Button " from my activity.xml but the MainActivity.kt do not resolve my id when i try to call it inside the code i tried to clean and re-build the project but still have the problem also invalidate caches restart

like this i want to call a the button from my acitivty_main.xml

button.setOnClickListener{ }

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.mostafa.stringlength.R.id.editText



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

button.(i here try to call the button) but shows me nothing //

    }

}

xml acitivty :

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="138dp"
        android:layout_height="58dp"
        android:layout_marginStart="136dp"
        android:layout_marginEnd="137dp"
        android:layout_marginBottom="212dp"
        android:text="أظهار النتيجة"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="411dp"
        android:layout_height="360dp"
        android:layout_marginStart="43dp"
        android:layout_marginTop="44dp"
        android:layout_marginEnd="43dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="ادخل النص هنا"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="417dp"
        android:layout_height="195dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/result"
            android:text="النتائج:"/>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 7

Views: 6815

Answers (6)

Felender
Felender

Reputation: 71

A sample solution

  val btn_start = findViewById<Button>(R.id.btnStart)
        btn_start.setOnClickListener{
            //Your code
        }

Upvotes: 2

Vitya Stroesku
Vitya Stroesku

Reputation: 91

Now, the plugins section is as follows:

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

Upvotes: 9

user6873176
user6873176

Reputation:

Kotlin Android Extensions have been deprecated. You can use Data binding or view binding offered by Jetpack.

You just need to include these line in your Module build.gradle file:

android {
...    

buildFeatures {
    viewBinding true
}

}

A binding object based on your Activity name will be generated e.g MainActivity will generate ActivityMainBinding

private lateinit var binding: ActivityMainBinding

You can now use this binding variable to inflate the view and reference the views in the layout:

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)

    binding.result.text = "My “Result!"
}

}

Upvotes: 1

itfayeci
itfayeci

Reputation: 1

There is a better way called View Bindging. It makes things much easier. When you use it you won't need 'kotlin-android-extensions'.

https://developer.android.com/topic/libraries/view-binding#java

Upvotes: 0

AbhayBohra
AbhayBohra

Reputation: 2117

Make sure you have this 2 lines at the top of your gradle file

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

and put

import kotlinx.android.synthetic.main.activity_main.* in your activity file

Upvotes: 9

sadat
sadat

Reputation: 4342

Add the following import statement at the top

import kotlinx.android.synthetic.main. activity_main.*

Upvotes: 0

Related Questions