Membuat Aplikasi Kalkulator dengan Android Studio





Nama : Yulianto Nugroho
NIM    : 2014230070
Teknik Informatika Unsada



Berikut pembuatan design layout gambar:




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/masukan1"
android:onClick="clearInput1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_toStartOf="@+id/operan"
android:layout_alignTop="@+id/operan"
android:layout_alignBottom="@+id/operan"
android:layout_toLeftOf="@+id/operan" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" "
android:id="@+id/operan"
android:textSize="30dp"
android:gravity="center"
android:layout_below="@+id/hasil"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/masukan2"
android:layout_toEndOf="@+id/operan"
android:layout_alignBottom="@+id/operan"
android:onClick="clearInput2"
android:layout_alignTop="@+id/operan"
android:layout_toRightOf="@+id/operan" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hasil ="
android:id="@+id/textView3"
android:textSize="16dp"
android:textAlignment="center"
android:textColor="#0bb798"
android:layout_alignTop="@+id/hasil"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/hasil"
android:gravity="right"
android:textSize="20dp"
android:textAlignment="center"
android:layout_marginTop="75dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" + "
android:id="@+id/tambah"
android:onClick="klikTambah"
android:layout_below="@+id/masukan1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/kurang"
android:onClick="klikKurang"
android:layout_alignTop="@+id/bagi"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/hasil"
android:layout_alignRight="@+id/tambah"
android:layout_alignEnd="@+id/tambah" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="@+id/kali"
android:onClick="klikKali"
android:layout_alignTop="@+id/tambah"
android:layout_alignLeft="@+id/bagi"
android:layout_alignStart="@+id/bagi" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="@+id/bagi"
android:onClick="klikBagi"
android:layout_below="@+id/kali"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hitung"
android:id="@+id/hitung"
android:onClick="klikHasil"
android:layout_alignBottom="@+id/bagi"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/kali" />
<TextView
android:id="@+id/notifikasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="Yulianto Nugroho" />
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/hasil"
android:layout_marginBottom="16dp"
android:layout_toEndOf="@+id/tambah"
android:layout_toRightOf="@+id/tambah"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</RelativeLayout>


------------------------------------------------------------------------------------------












Berikut Script lengkap pada MainActivity.java :


package com.example.jyuel.yuliantocalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView input1, input2, operator, result, notif;
    Button btHitung, btJumlah, btKurang, btKali, btBagi;

    private int operation = 0;
    private double HasilAkhir = 0.0;
    private String Cek1 = "";
    private String Cek2 = "";

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input1 = (TextView) findViewById(R.id.masukan1);
        input2 = (TextView) findViewById(R.id.masukan2);
        operator = (TextView) findViewById(R.id.operan);
        result = (TextView) findViewById(R.id.hasil);
        notif = (TextView) findViewById(R.id.notifikasi);

        btHitung = (Button) findViewById(R.id.hitung);
        btJumlah = (Button) findViewById(R.id.tambah);
        btKurang = (Button) findViewById(R.id.kurang);
        btKali = (Button) findViewById(R.id.kali);
        btBagi = (Button) findViewById(R.id.bagi);

    }

    public void klikTambah(View V){
        operation = 1;
        operator.setText("+");
    }

    public void klikKurang(View V){
        operation = 2;
        operator.setText("-");
    }

    public void klikKali(View V){
        operation = 3;
        operator.setText("x");
    }

    public void klikBagi(View V){
        operation = 4;
        operator.setText(":");
    }

    public void klikHasil(View V){

        Cek1 = input1.getText().toString();
        Cek2 = input2.getText().toString();

        if((Cek1.equalsIgnoreCase("")) || (Cek2.equalsIgnoreCase(""))){
            notif.setText("Kolom tidak boleh kosong");
        }
        else{
            double inputA = Double.parseDouble(input1.getText().toString());
            double inputB = Double.parseDouble(input2.getText().toString());

            switch(operation){
                case 1:
                    HasilAkhir = inputA + inputB;
                    break;

                case 2:
                    HasilAkhir = inputA - inputB;
                    break;

                case 3:
                    HasilAkhir = inputA * inputB;
                    break;

                case 4:
                    HasilAkhir = inputA / inputB;
                    break;

                case 0:
                    notif.setText("Pilih operasi perhitungan dahulu!");
                    break;

                default:
                    notif.setText("Undescribeable Error!");
                    break;
            }
            if(operation < 1){
                result.setText("0");
            }

            else{
                String hasilString = String.valueOf(HasilAkhir);
                result.setText(hasilString);
                notif.setText("Berhasil");
            }
        }

    }
    public void clearInput1(View V){
        input1.setText("");
        operation = 0;
    }
    public void clearInput2(View V){
        input2.setText("");
        operation = 0;
    }

}


-------------------------------------------------------------------------------------------


Berikut ini adalah gambar emulatornya :












Komentar