I was doing an APP following this tutorial --- https://www.youtube.com/watch?v=lTyyNpY7hy0
Ofc the code was deprecated since it uses android.extensions, so i decided to go with the updated implementation by using viewBinding
however the after running the buttons are not working and thus text is not being displayed in the output
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.thomas.calculadoracompleta.databinding.ActivityMainBinding
import org.mariuszgromada.math.mxparser.Expression
import java.lang.Exception
import java.text.DecimalFormat
private lateinit var binding: ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.buttonClear.setOnClickListener {
binding.input.text = ""
binding.output.text = ""
}
binding.buttonBracketLeft.setOnClickListener {
binding.input.text = addToInputText("(")
}
binding.buttonBracketRight.setOnClickListener {
binding.input.text = addToInputText(")")
}
binding.buttonDivision.setOnClickListener {
binding.input.text = addToInputText("÷")
}
binding.button7.setOnClickListener {
binding.input.text = addToInputText("7")
}
binding.button8.setOnClickListener {
binding.input.text = addToInputText("8")
}
binding.button9.setOnClickListener {
binding.input.text = addToInputText("9")
}
binding.buttonMultiply.setOnClickListener {
binding.input.text = addToInputText("×")
}
binding.button4.setOnClickListener {
binding.input.text = addToInputText("4")
}
binding.button5.setOnClickListener {
binding.input.text = addToInputText("5")
}
binding.button6.setOnClickListener {
binding.input.text = addToInputText("6")
}
binding.buttonSubtraction.setOnClickListener {
binding.input.text = addToInputText("-")
}
binding.button5.setOnClickListener {
binding.input.text = addToInputText("1")
}
binding.button4.setOnClickListener {
binding.input.text = addToInputText("2")
}
binding.button3.setOnClickListener {
binding.input.text = addToInputText("3")
}
binding.buttonAddition.setOnClickListener {
binding.input.text = addToInputText(" ")
}
binding.button0.setOnClickListener {
binding.input.text = addToInputText("0")
}
binding.buttonDot.setOnClickListener {
binding.input.text = addToInputText(".")
}
binding.buttonEquals.setOnClickListener {
showResult()
}
}
private fun addToInputText(buttonValue: String): String {
return "${binding.input.text}$buttonValue"
}
private fun getInputExpression(): String {
var expression = binding.input.text.replace(Regex("÷"), "/")
expression = expression.replace(Regex("×"),"*")
return expression
}
private fun showResult(){
try {
val expression = getInputExpression()
val result = Expression(expression).calculate()
if(result.isNaN()){
binding.output.text = "Error"
} else {
binding.output.text = DecimalFormat("0.######").format(result).toString()
}
} catch (e: Exception){
}
}
the problem is that buttons aren't working.
i'll add the 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:orientation="vertical"
android:background="@color/window_background"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:padding="30dp"
android:background="@color/io_background"
android:orientation="vertical">
<TextView
android:id="@ id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textSize="30sp"
android:textColor="@color/white"
tools:text="5 10-3" />
<TextView
android:id="@ id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textSize="50sp"
android:textColor="@color/green"
tools:text="12" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_clear"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="C"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_bracket_left"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="("/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_bracket_right"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text=")"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_division"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="÷"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_7"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="7" />
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_8"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="8"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_9"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="9"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_multiply"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="×"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_4"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="4"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_5"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="5"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_6"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="6"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_subtraction"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="-"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_1"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="1"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_2"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="2"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_3"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="3"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_addition"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text=" "/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_0"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="0"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_dot"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="."/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button_equals"
android:layout_width="0dp"
android:layout_height= "90dp"
android:layout_weight="1"
style="@style/Button_Style"
android:text="="/>
</TableRow>
</TableLayout>
</LinearLayout>
´´´
CodePudding user response:
put your binding inside activity class
class MainActivity : AppCompatActivity() {
// Here
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
...
CodePudding user response:
Actually it was a XML problem fixed it after and now everything is working perfectly.