skip to Main Content

I am facing a problem which is my button ID is well-configured and in my main activity, I am calling the button using the correct ID too, but after I run the project on the emulator, the button is not working after I clicked on it for multiple times.

Can anyone help me to solve the problem?

This is my xml file:

<?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="@color/bgColor"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="20dp"
        android:text="Mobile Quiz Application"
        android:textColor="@color/white"
        android:textSize="30sp"
        android:textStyle="bold" />

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        app:cardCornerRadius="10dp"
        android:background="@color/white">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Welcome To The Quiz App!"
                android:textColor="@color/txtTitle"
                android:textSize="25sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Please enter your name before starting the quiz."
                android:textColor="@color/txtDesc"
                android:textSize="15sp" />

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

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/etName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your name......"
                    android:inputType="textCapWords"
                    android:textColor="@color/txtTitle"
                    android:textColorHint="@color/txtDesc" />
            </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="15dp"
                android:text="Continue"
                android:textStyle="bold">
            </Button>
        </LinearLayout>
    </com.google.android.material.card.MaterialCardView>
</LinearLayout>

This is my main file:

package com.example.quizapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

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

        val btnStart = findViewById<Button>(R.id.btn_Start)
        val nameInput = findViewById<EditText>(R.id.etName)
        btnStart.setOnClickListener {
            Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()

        }
    }
}

2

Answers


  1. findViewById is an old method.
    i would recommend you to use with ViewBinding.
    click here to documentation

    Login or Signup to reply.
  2. You can use viewBinding

    add line to build.gradle

      buildFeatures{
        viewBinding true
    }
    

    and update your activity like this

     class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    
        binding.btnStart.setOnClickListener{
          Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search