Home > database >  Kotlin : how to pass the numeric value to method and show with Toast.makeText( )
Kotlin : how to pass the numeric value to method and show with Toast.makeText( )

Time:06-12

I made four simple buttons. When the user clicks the button, Toast (pop up message) comes out. The sentence showing up is 'Button is clicked'

I want to modify the text to follow this form. 'The $num button is clicked'

actual result button is clicked

Here is my kotlin file code

package com.example.mycalculator_chapter7

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

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

    fun onDigit(view: View, num:Int) {
        Toast.makeText(this, "$num Button is clicked", Toast.LENGTH_LONG).show()

    }
}

Here is my activity_main xml file code

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@ id/btnSeven"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="@string/buttonSeven"
            android:onClick="onDigit(7)"
            tools:ignore="OnClick,UsingOnClickInXml"/>
        <Button
            android:id="@ id/btnEight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="@string/buttonEight"
            android:onClick="onDigit(8)"
            tools:ignore="OnClick,UsingOnClickInXml"/>
        <Button
            android:id="@ id/btnNine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="@string/buttonNine"
            android:onClick="onDigit(9)"
            tools:ignore="OnClick,UsingOnClickInXml"/>
        <Button
            android:id="@ id/btnDivide"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="@string/buttonZero"
            android:onClick="onDigit(0)"
            tools:ignore="OnClick,UsingOnClickInXml" />
    </LinearLayout>

question i know that using onClickEventListener is better than using onClick, but i want to know how to deliver the numeric value to method 'onDigit' in Class, showing the result of the number.

CodePudding user response:

You have the number inside the button, so just use it
e.g:

Button btnSeven = findViewById(R.id.btnSeven)

btnSeven.setOnClickListener { 
     val msg = "${btnSeven.text} Button is clicked"
     Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
}

CodePudding user response:

Solution 1 with onClick: This code will do what you want, I add a check to make sure that the View is a Button so I can have access to the text of the button, then I get the num from the text of the button and convert it to string: val num = view.text.toString()

package com.example.mycalculator_chapter7

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

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

    fun onDigit(view: View) {
        if (view !is Button) return
        val num = view.text.toString()
        Toast.makeText(this, "$num Button is clicked", Toast.LENGTH_LONG).show()
    }
}

Solution 2 with setOnClickListener: This is the best way to add on click events, first you need to remove all onClick lines from xml, then add click listener to each button by kotlin:

package com.example.mycalculator_chapter7

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

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

        btnSeven.setOnClickListener { onDigit(7) }
        btnEight.setOnClickListener { onDigit(8) }
        btnNine.setOnClickListener { onDigit(9) }
    }

    fun onDigit(num: Int) {
        Toast.makeText(this, "$num Button is clicked", Toast.LENGTH_LONG).show()
    }
}
  • Related