Home > Software engineering >  editableText is not working in android studio
editableText is not working in android studio

Time:06-10

I want to take Input from user(their name) with editableText just like the below video(watch at 8:24). But, I am not able to take input because android studio is not recognizing "editableText". Am i missing something?

Android Development Tutorial Video at 8:24

Error ->> Unresolved reference: nameInput [In the below MainActivity.kt]

MainActivity.kt

package com.example.firstapp

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

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun createBirthdayCard(view: View){
        val name = nameInput.**editableText**.toString()  <-- [error is in this line]
    }
}

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

    <TextView
        android:id="@ id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="140dp"
        android:text="Enter Name"
        android:textColor="#000000"
        android:textSize="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:textAlignment="center" />

    <EditText
        android:id="@ id/nameInput"
        android:layout_width="280dp"
        android:layout_height="54dp"
        android:layout_marginTop="64dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView3"
        tools:textAlignment="center" />

    <Button
        android:id="@ id/createBirthdayCard"
        android:layout_width="307dp"
        android:layout_height="81dp"
        android:layout_marginTop="144dp"
        android:maxLines="1"
        android:text="Create Birthday Card"
        android:textSize="19sp"
        android:onClick="createBirthdayCard"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/nameInput"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

:) :) Thanks for spending your valuable time to help me :) :)

CodePudding user response:

Android kotlin extensions removed at Android studio 4.1V.

I recommend use the ViewBinding or DataBinding.

This is a good reference.

https://android-developers.googleblog.com/2020/11/the-future-of-kotlin-android-extensions.html

ViewBinding : https://developer.android.com/topic/libraries/view-binding?hl=ko

DataBinding : https://developer.android.com/topic/libraries/data-binding

CodePudding user response:

As Android kotlin extensions is deprecated and removed you may use View binding, Data binding as suggested by Seungho Kang

OR you can declare variable as shown below.

fun createBirthdayCard(view: View){
       val editText=findViewById<EditText>(R.id.nameInput)
       val name = editText.editableText.toString()
    }
  • Related