Here we have my first Calculator in Android Studio
Start Layout
Suma
Resultado
Code backup
buttons_layouut.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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="4"
android:rowCount="5">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#DCB3B3"
android:onClick="borrar"
android:text="CA"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2px" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#DCB3B3"
android:onClick="borrar"
android:text="C"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2px" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#DCB3B3"
android:onClick="cambiarOperador"
android:text="%"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2px" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#5D0202"
android:onClick="cambiarOperador"
android:text="÷"
android:textSize="34sp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="7"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="8"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="9"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#5D0202"
android:onClick="cambiarOperador"
android:text="x"
android:textColor="@color/white"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="2"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="4"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="2"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="5"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="2"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="6"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="2"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#5D0202"
android:onClick="cambiarOperador"
android:text="-"
android:textColor="@color/white"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="3"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="1"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="3"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="2"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button15"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="3"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="3"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button16"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="3"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#5D0202"
android:onClick="cambiarOperador"
android:text="+"
android:textColor="@color/white"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="4"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="0"
android:textAlignment="viewStart"
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button18"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="4"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="@color/white"
android:onClick="selectNumbers"
android:text="."
android:textColor="#5D0202"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
<Button
android:id="@+id/button20"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_row="4"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:backgroundTint="#5D0202"
android:onClick="igual"
android:text="="
android:textColor="@color/white"
android:textSize="34sp"
app:strokeColor="#5D0202"
app:strokeWidth="2dp" />
</GridLayout>
</LinearLayout>
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAlignment="textEnd"
android:textColor="#7B090000"
android:textSize="34dp" />
<TextView
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAlignment="textEnd"
android:textSize="60sp" />
<include
layout="@layout/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.kt
package com.example.mycalqlator
import android.icu.text.DecimalFormat
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
val SUMA = "+"
val RESTA = "-"
val MULTIPLICACION = "*"
val DIVISION = "/"
val PORCENTAJE = "%"
var OpActual = ""
var primerNumero:Double = Double.NaN
var segundoNumero:Double = Double.NaN
lateinit var txtTemp:TextView
lateinit var txtResult:TextView
lateinit var formatoDecimal: DecimalFormat
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
formatoDecimal = DecimalFormat("#.#########")
txtTemp = findViewById(R.id.txtTemp)
txtResult = findViewById(R.id.txtResult)
}
fun cambiarOperador(b: View){
if(txtTemp.text.isNotEmpty() || primerNumero.toString()!="NaN") {
calqlar()
val boton: Button = b as Button
if (boton.text.toString().trim() == "÷") {
OpActual = "/"
} else if (boton.text.toString().trim() == "x") {
OpActual = "*"
} else {
OpActual = boton.text.toString().trim()
}
txtResult.text = formatoDecimal.format(primerNumero) + OpActual
txtTemp.text = ""
}
}
fun calqlar(){
try {
if (primerNumero.toString() != "NaN") {
if (txtTemp.text.toString().isEmpty()) {
txtTemp.text = txtResult.text.toString()
}
segundoNumero = txtTemp.text.toString().toDouble()
txtTemp.text = ""
when (OpActual) {
"+" -> primerNumero = (primerNumero + segundoNumero)
"-" -> primerNumero = (primerNumero - segundoNumero)
"*" -> primerNumero = (primerNumero * segundoNumero)
"/" -> primerNumero = (primerNumero / segundoNumero)
"%" -> primerNumero = (primerNumero % segundoNumero)
}
} else {
primerNumero = txtTemp.text.toString().toDouble()
}
}catch (e:Exception){
}
}
fun selectNumbers(b: View) {
val boton: Button = b as Button
val buttonText = boton.text.toString()
val currentText = txtTemp.text.toString()
when {
buttonText == "." -> {
if (currentText.contains(".")) return
txtTemp.text = if (currentText.isEmpty()) "0." else currentText + "."
}
currentText == "0" -> {
if (buttonText != "0") {
txtTemp.text = buttonText
}
}
else -> {
txtTemp.text = currentText + buttonText
}
}
}
fun igual(b: View){
calqlar()
txtResult.text = formatoDecimal.format(primerNumero)
OpActual = ""
}
fun borrar(b:View){
val boton:Button = b as Button
if(boton.text.toString().trim()=="C"){
if(txtTemp.text.toString().isNotEmpty()){
var datosActuales:CharSequence = txtTemp.text as CharSequence
txtTemp.text = datosActuales.subSequence(0, datosActuales.length - 1)
}else{
primerNumero = Double.NaN
segundoNumero = Double.NaN
txtTemp.text = ""
txtResult.text = ""
}
}else if(boton.text.toString().trim() == "CA"){
primerNumero = Double.NaN
segundoNumero = Double.NaN
txtTemp.text = ""
txtResult.text = ""
}
}
}
Top comments (0)