Home > database >  Why is AppCompatEditText text not being accessed when called in setOnCLickListener in MainActivity.k
Why is AppCompatEditText text not being accessed when called in setOnCLickListener in MainActivity.k

Time:11-30

I was referring an YouTube video for creating a quiz app. I was copying exactly but still my code doesn't work.

In the activity_main.xml I put an AppCompatEditText inside a TextInputLayout. In the MainActivity.kt, I tried to access the text in the EditText, but it seems that no matter what I do the text remains null, though we can see it up there in the text box on screen.

MainActivity.kt :

package com.example.quizapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.quizapp.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    lateinit var binding : ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN

        btn_start.setOnClickListener {
            if (et_name.text.toString().isEmpty()) {
                Toast.makeText(this@MainActivity, "Please enter a name", Toast.LENGTH_SHORT).show()
            } else {
                val intent = Intent(this@MainActivity, QuizQuestionsActivity::class.java)
                startActivity(intent)
                finish()
            }
        }
    }
}

I tried to change the if statement to if(et_name?.text.toString().isEmpty()), but that would make the code skip to the else statement.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@drawable/ic_bg"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@ id/tv_app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:gravity="center"
        android:text="@string/quizapp"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold" />

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:background="@color/white"
        app:cardCornerRadius="8dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/welcome"
                android:textColor="@color/dark_grey"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/please_enter_your_name"
                android:textColor="@color/light_grey"
                android:textSize="16sp" />

            <com.google.android.material.textfield.TextInputLayout
                android:id="@ id/til_name"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@ id/et_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="actionGo"
                    android:hint="@string/name"
                    android:inputType="textCapWords"
                    android:textColorHint="@color/light_grey"
                    android:textColor="@color/dark_grey" />

            </com.google.android.material.textfield.TextInputLayout>

            <Button
                android:id="@ id/btn_start"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@color/purple_500"
                android:text="@string/start"
                android:textColor="@color/white"
                android:textSize="18sp">

            </Button>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

The YouTube video : Android masterclass: building a quiz app. His drive link: His code for the app.

GitHub repo to my app: QuizApp

CodePudding user response:

binding.btn_start.setOnClickListener{
            if(binding.et_name.text.isBlank()) {
                Toast.makeText(this, "Please enter a name", Toast.LENGTH_SHORT).show()
            } else {
                val intent = Intent(this, QuizQuestionsActivity::class.java)
                startActivity(intent)
                finish()
            }
        }

by the way,you should use viewBinding, kotlin extension was deprecation,it may cause NullPoint Exception

  • Related