Dalam tutorial kali ini, saya akan sharing kode membuat Aplikasi Kalkulator dengan Android Studio, XML dan Java. Aplikasi ini hanya membutuhkan 1 Activity dan Layout. Berikut kodenya
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="angka1"
android:inputType="number" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="angka2"
android:inputType="number" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hapus" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
MainActivity.java
package com.webhozz.android.kalkulator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editAngka1, editAngka2;
TextView tvHasil;
Button tambah,kurang,bagi,kali,hapus;
Integer angka1, angka2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editAngka1 = (EditText) findViewById(R.id.editText1);
editAngka2 = (EditText) findViewById(R.id.editText2);
tvHasil = (TextView) findViewById(R.id.textView2);
tambah = (Button) findViewById(R.id.button1);
kurang = (Button) findViewById(R.id.button2);
bagi = (Button) findViewById(R.id.button3);
kali = (Button) findViewById(R.id.button4);
hapus = (Button) findViewById(R.id.button5);
tambah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HasilTambah();
}
});
kurang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HasilKurang();
}
});
bagi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HasilBagi();
}
});
kali.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HasilKali();
}
});
hapus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hapus();
}
});
}
public void HasilTambah() {
if (editAngka1.getText().toString().isEmpty()) {
Toast.makeText(this, "Mohon isi form Angka 1", Toast.LENGTH_SHORT).show();
}
else if (editAngka2.getText().toString().isEmpty()) {
Toast.makeText(this, "Mohon isi form Angka 2", Toast.LENGTH_SHORT).show();
}
else {
angka1 = Integer.parseInt(editAngka1.getText().toString());
angka2 = Integer.parseInt(editAngka2.getText().toString());
Integer hasil = angka1 + angka2;
tvHasil.setText(String.valueOf(hasil));
}
}
public void HasilKurang(){
if (!editAngka1.getText().toString().isEmpty()){
Toast.makeText(this,"Mohon isi angka pada form pertama",Toast.LENGTH_SHORT).show();
}
if (!editAngka2.getText().toString().isEmpty()){
Toast.makeText(this, "Mohon isi angka pada form kedua",Toast.LENGTH_SHORT).show();
}
try {
angka1 = Integer.parseInt(editAngka1.getText().toString());
}catch (Exception e){
Toast.makeText(this, "Mohon isi angka pada form pertama",Toast.LENGTH_SHORT).show();
}
try {
angka2 = Integer.parseInt(editAngka2.getText().toString());
}catch (Exception e){
Toast.makeText(this, "Mohon Isi angka pada form kedua",Toast.LENGTH_SHORT).show();
}
Integer hasil = angka1 - angka2;
tvHasil.setText(String.valueOf(hasil));
}
public void HasilBagi(){
if (!editAngka1.getText().toString().isEmpty()){
Toast.makeText(this,"Mohon isi angka pada form pertama",Toast.LENGTH_SHORT).show();
}
if (!editAngka2.getText().toString().isEmpty()){
Toast.makeText(this, "Mohon isi angka pada form kedua",Toast.LENGTH_SHORT).show();
}
try {
angka1 = Integer.parseInt(editAngka1.getText().toString());
}catch (Exception e){
Toast.makeText(this, "Mohon isi angka pada form pertama",Toast.LENGTH_SHORT).show();
}
try {
angka2 = Integer.parseInt(editAngka2.getText().toString());
}catch (Exception e){
Toast.makeText(this, "Mohon Isi angka pada form kedua",Toast.LENGTH_SHORT).show();
}
Integer hasil = angka1 / angka2;
tvHasil.setText(String.valueOf(hasil));
}
public void HasilKali(){
if (!editAngka1.getText().toString().isEmpty()){
Toast.makeText(this,"Mohon isi angka pada form pertama",Toast.LENGTH_SHORT).show();
}
if (!editAngka2.getText().toString().isEmpty()){
Toast.makeText(this, "Mohon isi angka pada form kedua",Toast.LENGTH_SHORT).show();
}
try {
angka1 = Integer.parseInt(editAngka1.getText().toString());
}catch (Exception e){
Toast.makeText(this, "Mohon isi angka pada form pertama",Toast.LENGTH_SHORT).show();
}
try {
angka2 = Integer.parseInt(editAngka2.getText().toString());
}catch (Exception e){
Toast.makeText(this, "Mohon Isi angka pada form kedua",Toast.LENGTH_SHORT).show();
}
Integer hasil = angka1 * angka2;
tvHasil.setText(String.valueOf(hasil));
}
public void hapus(){
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
this.finish(); //
}
}