Caculator app in android, how to design calculator app
Main activity.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:clipToPadding="false"
android:orientation="vertical"
android:padding="10dp"
app:barrierMargin="20dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="387dp"
android:layout_height="159dp"
app:srcCompat="@drawable/cal"
android:contentDescription="TODO" />
<EditText
android:id="@+id/etFirstvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_first_number"
android:inputType="number"
android:importantForAutofill="no" />
<EditText
android:id="@+id/etSecondvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_second_number"
android:inputType="number"
android:autofillHints="" />
<TextView
android:id="@+id/tvAns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="40dp" />
<Button
android:id="@+id/btnSum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/plus"
android:textStyle="bold" />
<Button
android:id="@+id/btnSubtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/minus"
android:textStyle="bold" />
<Button
android:id="@+id/btnMultiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/multiply"
android:textStyle="bold" />
<Button
android:id="@+id/btnDevision"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/devision"
android:textStyle="bold" />
</LinearLayout>
Main Activity java code
package com.example.calculatorme;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
EditText etFirstvalue, etSecondvalue;
TextView tvAns;
Button btnSum,btnSubtract,btnMultiply,btnDevision;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFirstvalue= findViewById(R.id.etFirstvalue);
etSecondvalue= findViewById(R.id.etSecondvalue);
tvAns= findViewById(R.id.tvAns);
btnSum= findViewById(R.id.btnSum);
btnSubtract= findViewById(R.id.btnSubtract);
btnMultiply= findViewById(R.id.btnMultiply);
btnDevision= findViewById(R.id.btnDevision);
btnSum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int firstvalue, secondvalue, ans;
firstvalue= Integer.parseInt(etFirstvalue.getText().toString());
secondvalue= Integer.parseInt(etSecondvalue.getText().toString());
ans = firstvalue + secondvalue;
tvAns.setText("Ans is" + ans);
}
});
btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int firstvalue, secondvalue, ans;
firstvalue= Integer.parseInt(etFirstvalue.getText().toString());
secondvalue= Integer.parseInt(etSecondvalue.getText().toString());
ans = firstvalue - secondvalue;
tvAns.setText("Ans is" + ans);
}
});
btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int firstvalue, secondvalue, ans;
firstvalue= Integer.parseInt(etFirstvalue.getText().toString());
secondvalue= Integer.parseInt(etSecondvalue.getText().toString());
ans = firstvalue * secondvalue;
tvAns.setText("Ans is" + ans);
}
});
btnDevision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int firstvalue, secondvalue, ans;
firstvalue= Integer.parseInt(etFirstvalue.getText().toString());
secondvalue= Integer.parseInt(etSecondvalue.getText().toString());
ans = firstvalue / secondvalue;
tvAns.setText("Ans is" + ans);
}
});
}
}
Step 3: Paste a calculator icon in
drawable folder Android Development