programmer pro
programmer pro

Reputation: 179

Kotlin onClickListener on button not working

I am very new to Android Development. I am currently making a calculator with Kotlin in android studio. I am have currently 9 buttons with numerical values(1, 2, 3, 4, 5, 6, 7, 8, 9) I want when i click the button the text on the textView changes according to the values on the button. When i click the button the text on textView changes, but, there is strange bug. Like, when i click on 7 first time it does not add the text, and then if i click on some other button like 8 instead of adding 8 it adds 7 to the textView and then if i click on say 5, it adds 8 instead of 5. My MainActivity.kt code is :-

package com.example.calculator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        num1.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "1")
        }

        num2.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "2")
        }

        num3.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "3")
        }

        num4.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "4")
        }

        num5.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "5")
        }

        num6.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "6")
        }

        num7.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "7")
        }

        num8.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "8")
        }

        num9.setOnClickListener {
            mainText.setText(mainText.getText().toString() + "9")
        }

    }

}

and my activity_main.xml code is :-

<?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/mainText"
        android:layout_width="408dp"
        android:layout_height="68dp"
        android:background="#f2f2f2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.333"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textDirection="rtl"
        android:paddingRight="20sp"
        android:textSize="25sp"/>

    <!-- First Row-->
    <Button
        android:id="@+id/num9"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:text="9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/num8"
        app:layout_constraintTop_toTopOf="@+id/num8" />

    <Button
        android:id="@+id/num8"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:layout_marginStart="8dp"
        android:text="8"
        app:layout_constraintBottom_toBottomOf="@+id/num7"
        app:layout_constraintStart_toEndOf="@+id/num7" />

    <Button
        android:id="@+id/num7"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:text="7"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mainText" />

    <!-- Second Row -->

    <Button
        android:id="@+id/num6"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:text="6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/num5"
        app:layout_constraintTop_toTopOf="@+id/num5" />

    <Button
        android:id="@+id/num5"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:layout_marginStart="10dp"
        android:text="5"
        app:layout_constraintBottom_toBottomOf="@+id/num4"
        app:layout_constraintStart_toEndOf="@+id/num4" />

    <Button
        android:id="@+id/num4"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="10dp"
        android:text="4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/num7" />

    <!-- Third Row -->

    <Button
        android:id="@+id/num3"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:text="3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/num2"
        app:layout_constraintTop_toTopOf="@+id/num2" />

    <Button
        android:id="@+id/num2"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:layout_marginStart="10dp"
        android:text="2"
        app:layout_constraintBottom_toBottomOf="@+id/num1"
        app:layout_constraintStart_toEndOf="@+id/num1" />

    <Button
        android:id="@+id/num1"
        android:layout_width="110dp"
        android:layout_height="54dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:text="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/num4" />

</androidx.constraintlayout.widget.ConstraintLayout>

I am very confused why this bug is occuring and what is the problem in my code. Thanks in Advance :-

Upvotes: 1

Views: 1169

Answers (3)

MMG
MMG

Reputation: 3268

You save text after every click but you show previous saved text. If you want to see changes of textview instantly, you should use textwatcher

Upvotes: 0

Try this
Try this

Reputation: 621

I tried to reproduce this with exact same code. But no luck seem to be working fine (see attached GIF). Not sure what is the problem at you end but you can try posting setText call on UI Thread (onClick is actually UI thread but worth trying to post UI update on Main Thread) or you can call requestLayout on text view reference.

enter image description here

  1. Post on UI thread:
runOnUiThread {
    mainText.setText(mainText.getText().toString() + "9")
}
  1. Call requestLayout of TextView
mainText.setText(mainText.getText().toString() + "8")
mainText.requestLayout()

Again I would like to reiterate that stated issue is not reproducible at my end and these are just suggestions you can try. Thanks.

Upvotes: 3

isthemartin
isthemartin

Reputation: 126

I tested your code and it's working well. So, I optimized a little bit your work, I hope that this works for you:

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

    num1.setOnClickListener(this)
    num2.setOnClickListener(this)
    num3.setOnClickListener(this)
    num4.setOnClickListener(this)
    num5.setOnClickListener(this)
    num6.setOnClickListener(this)
    num7.setOnClickListener(this)
    num8.setOnClickListener(this)
    num9.setOnClickListener(this)
}

override fun onClick(view: View?) {
    val currentValue = mainText.text
    val numberPressed = (view as Button).text
    mainText.setText("${currentValue}${numberPressed}")
}
}

Upvotes: 1

Related Questions