Calculator
Xml Code:
<?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"
tools:ignore="MissingClass">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:background="@drawable/border"
android:padding="10dp"
android:text="Demo Calculator"
android:textColor="@color/black"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/ed_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="@+id/ed_1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/ed_1"
app:layout_constraintTop_toBottomOf="@+id/ed_1" />
<TextView
android:id="@+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:padding="5dp"
android:text="Result"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="@+id/btn_sum"
app:layout_constraintHorizontal_bias="0.181"
app:layout_constraintStart_toStartOf="@+id/btn_sum"
app:layout_constraintTop_toBottomOf="@+id/btn_sum" />
<Button
android:id="@+id/btn_sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Add"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="@+id/ed_2"
app:layout_constraintHorizontal_bias="0.551"
app:layout_constraintStart_toStartOf="@+id/ed_2"
app:layout_constraintTop_toBottomOf="@+id/ed_2" />
<EditText
android:id="@+id/ed_1"
android:layout_width="205dp"
android:layout_height="62dp"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/btn_multi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multi"
android:textSize="24sp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="48dp"
tools:layout_editor_absoluteY="323dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java Code:
package com.example.uicon;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText ed_1 = findViewById(R.id.ed_1);
private EditText ed_2 = findViewById(R.id.ed_2);
private Button btn_sum = findViewById(R.id.btn_sum);
private TextView txt_result = findViewById(R.id.txt_result);
private Button btn_multi = findViewById(R.id.btn_multi);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_sum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1=Integer.parseInt(ed_1.getText().toString());
int num2=Integer.parseInt(ed_2.getText().toString());
int sum=num1+num2;
String Result=String.valueOf(sum);
txt_result.setText(Result);
}
});
btn_multi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1=Integer.parseInt(ed_1.getText().toString());
int num2=Integer.parseInt(ed_2.getText().toString());
int multiply=num1*num2;
txt_result.setText(multiply);
}
});
}
}