Calculator
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number 1"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/num1"
android:hint="Number 2"
android:inputType="numberDecimal" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/num2"
android:text="+" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b1"
android:text="-" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b2"
android:text="×" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b3"
android:text="÷" />
<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b4"
android:layout_marginTop="10dp"
android:text="Result:"
android:textSize="16sp" />
</RelativeLayout>
JAVA
package com.msbte.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
EditText num1, num2;
TextView result;
Button btnAdd, btnSub, btnMul, btnDiv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
result = findViewById(R.id.txtResult);
btnAdd = findViewById(R.id.b1);
btnSub = findViewById(R.id.b2);
btnMul = findViewById(R.id.b3);
btnDiv = findViewById(R.id.b4);
btnAdd.setOnClickListener(v -> calculate('+'));
btnSub.setOnClickListener(v -> calculate('-'));
btnMul.setOnClickListener(v -> calculate('*'));
btnDiv.setOnClickListener(v -> calculate('/'));
}
private void calculate(char operator) {
try {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = 0;
switch (operator) {
case '+': res = n1 + n2; break;
case '-': res = n1 - n2; break;
case '*': res = n1 * n2; break;
case '/':
if (n2 != 0) res = n1 / n2;
else {
result.setText("Cannot divide by zero");
return;
}
break;
}
result.setText("Result: " + res);
} catch (NumberFormatException e) {
result.setText("Enter valid numbers");
}
}
}
button 0 TO 9 submit clrar
XML
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp" android:stretchColumns="*">
<TextView
android:id="@+id/display" android:text="Clicked: "
android:layout_height="wrap_content" android:layout_width="match_parent"
android:textSize="18sp" />
<!-- Rows of buttons -->
<TableRow>
<Button android:id="@+id/b1" android:text="1"/>
<Button android:id="@+id/b2" android:text="2"/>
<Button android:id="@+id/b3" android:text="3"/>
</TableRow>
<TableRow>
<Button android:id="@+id/b4" android:text="4"/>
<Button android:id="@+id/b5" android:text="5"/>
<Button android:id="@+id/b6" android:text="6"/>
</TableRow>
<TableRow>
<Button android:id="@+id/b7" android:text="7"/>
<Button android:id="@+id/b8" android:text="8"/>
<Button android:id="@+id/b9" android:text="9"/>
</TableRow>
<TableRow>
<Button android:id="@+id/b0" android:text="0"/>
<Button android:id="@+id/submit" android:text="Submit"/>
<Button android:id="@+id/clear" android:text="Clear"/>
</TableRow>
</TableLayout>
JAVA
package com.msbte.num0to9;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
StringBuilder data = new StringBuilder();
TextView display;
Button submit,clear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.display);
submit=findViewById(R.id.submit);
clear=findViewById(R.id.clear);
int[] ids = {R.id.b0, R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8,
R.id.b9};
for (int id : ids)
{
findViewById(id).setOnClickListener(v ->
{
Button b = (Button) v;
data.append(b.getText());
});
}
submit.setOnClickListener(v -> display.setText("Clicked: " + data));
clear.setOnClickListener(v -> {
data.setLength(0);
display.setText("Clicked: ");
});
}
}