Halo, kali ini saya akan membahas sedikit tentang penggunaan Android Studio dengan membuat aplikasi kalkulator sederhana.
Ikuti saja langkah-langkah berikut
Pertama-tama, kita deklarasikan semua komponen dan fungsi layout yang kita gunakan menggunakan code seperti berikut.
package com.almer.alkalkulator;
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 {
private Button zero;
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private Button add;
private Button sub;
private Button mul;
private Button div;
private Button equal;
private Button clear;
private TextView info;
private TextView result;
private final char ADDITION = '+';
private final char SUBSTRACTION = '-';
private final char MULTIPLY = '*';
private final char DIVISION = '/';
private final char EQU = 0;
private double val1 = Double.NaN;
private double val2;
private char ACTION;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUIViews();
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "0");
}
});
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "9");
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = ADDITION;
result.setText(String.valueOf(val1) + "+");
info.setText(null);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = SUBSTRACTION;
result.setText(String.valueOf(val1) + "-");
info.setText(null);
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = MULTIPLY;
result.setText(String.valueOf(val1) + "*");
info.setText(null);
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = DIVISION;
result.setText(String.valueOf(val1) + "/");
info.setText(null);
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = EQU;
result.setText(result.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1));
info.setText(null);
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(info.getText().length() > 0){
CharSequence name = info.getText().toString();
info.setText(name.subSequence(0, name.length()-1));
}
else{
val1 = Double.NaN;
val2 = Double.NaN;
info.setText(null);
result.setText(null);
}
}
});
}
private void setupUIViews(){
zero = (Button)findViewById(R.id.btn0);
one = (Button)findViewById(R.id.btn1);
two = (Button)findViewById(R.id.btn2);
three = (Button)findViewById(R.id.btn3);
four = (Button)findViewById(R.id.btn4);
five = (Button)findViewById(R.id.btn5);
six = (Button)findViewById(R.id.btn6);
seven = (Button)findViewById(R.id.btn7);
eight = (Button)findViewById(R.id.btn8);
nine = (Button)findViewById(R.id.btn9);
add = (Button)findViewById(R.id.btnadd);
sub = (Button)findViewById(R.id.btnsub);
mul = (Button)findViewById(R.id.btnmul);
div = (Button)findViewById(R.id.btndiv);
equal = (Button)findViewById(R.id.btnequal);
clear = (Button)findViewById(R.id.btnclr);
info = (TextView)findViewById(R.id.tvctrl);
result = (TextView)findViewById(R.id.tvrslt);
}
private void compute(){
if(!Double.isNaN(val1)){
val2 = Double.parseDouble(info.getText().toString());
switch(ACTION){
case ADDITION:
val1 = val1 + val2;
break;
case SUBSTRACTION:
val1 = val1 - val2;
break;
case MULTIPLY:
val1 = val1 * val2;
break;
case DIVISION:
val1 = val1 / val2;
break;
case EQU:
break;
}
}
else{
val1 = Double.parseDouble(info.getText().toString());
}
}
}
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 {
private Button zero;
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private Button add;
private Button sub;
private Button mul;
private Button div;
private Button equal;
private Button clear;
private TextView info;
private TextView result;
private final char ADDITION = '+';
private final char SUBSTRACTION = '-';
private final char MULTIPLY = '*';
private final char DIVISION = '/';
private final char EQU = 0;
private double val1 = Double.NaN;
private double val2;
private char ACTION;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUIViews();
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "0");
}
});
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.setText(info.getText().toString() + "9");
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = ADDITION;
result.setText(String.valueOf(val1) + "+");
info.setText(null);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = SUBSTRACTION;
result.setText(String.valueOf(val1) + "-");
info.setText(null);
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = MULTIPLY;
result.setText(String.valueOf(val1) + "*");
info.setText(null);
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = DIVISION;
result.setText(String.valueOf(val1) + "/");
info.setText(null);
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compute();
ACTION = EQU;
result.setText(result.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1));
info.setText(null);
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(info.getText().length() > 0){
CharSequence name = info.getText().toString();
info.setText(name.subSequence(0, name.length()-1));
}
else{
val1 = Double.NaN;
val2 = Double.NaN;
info.setText(null);
result.setText(null);
}
}
});
}
private void setupUIViews(){
zero = (Button)findViewById(R.id.btn0);
one = (Button)findViewById(R.id.btn1);
two = (Button)findViewById(R.id.btn2);
three = (Button)findViewById(R.id.btn3);
four = (Button)findViewById(R.id.btn4);
five = (Button)findViewById(R.id.btn5);
six = (Button)findViewById(R.id.btn6);
seven = (Button)findViewById(R.id.btn7);
eight = (Button)findViewById(R.id.btn8);
nine = (Button)findViewById(R.id.btn9);
add = (Button)findViewById(R.id.btnadd);
sub = (Button)findViewById(R.id.btnsub);
mul = (Button)findViewById(R.id.btnmul);
div = (Button)findViewById(R.id.btndiv);
equal = (Button)findViewById(R.id.btnequal);
clear = (Button)findViewById(R.id.btnclr);
info = (TextView)findViewById(R.id.tvctrl);
result = (TextView)findViewById(R.id.tvrslt);
}
private void compute(){
if(!Double.isNaN(val1)){
val2 = Double.parseDouble(info.getText().toString());
switch(ACTION){
case ADDITION:
val1 = val1 + val2;
break;
case SUBSTRACTION:
val1 = val1 - val2;
break;
case MULTIPLY:
val1 = val1 * val2;
break;
case DIVISION:
val1 = val1 / val2;
break;
case EQU:
break;
}
}
else{
val1 = Double.parseDouble(info.getText().toString());
}
}
}
lalu buat kodingan .xml seperti code dibawah ini :
<RelativeLayout 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="com.almer.alkalkulator.MainActivity">
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn4"
android:layout_alignStart="@+id/tvctrl"
android:text="9" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn9"
android:layout_toEndOf="@+id/btn9"
android:text="8"
tools:text="8" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn9"
android:layout_toEndOf="@+id/btn8"
android:text="7"
tools:text="7" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn3"
android:layout_alignStart="@+id/btn9"
android:text="6"
tools:text="6" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn6"
android:layout_toEndOf="@+id/btn9"
android:text="5" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btn7"
android:layout_alignTop="@+id/btn6"
android:text="4"
tools:text="4" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnclr"
android:layout_alignStart="@+id/btn9"
android:text="3" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn3"
android:layout_toEndOf="@+id/btn9"
android:text="2" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btn7"
android:layout_alignTop="@+id/btn3"
android:text="1" />
<Button
android:id="@+id/btnclr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/btn9"
android:layout_marginBottom="52dp"
android:text="CLR" />
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnclr"
android:layout_toEndOf="@+id/btn9"
android:text="0" />
<Button
android:id="@+id/btnequal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnclr"
android:layout_alignStart="@+id/btn7"
android:text="=" />
<Button
android:id="@+id/btndiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/btn9"
android:layout_marginEnd="11dp"
android:text="/" />
<Button
android:id="@+id/btnmul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btndiv"
android:layout_alignTop="@+id/btn6"
android:text="*" />
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btndiv"
android:layout_alignTop="@+id/btn3"
android:text="+" />
<Button
android:id="@+id/btnsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btndiv"
android:layout_alignTop="@+id/btn0"
android:text="-" />
<TextView
android:id="@+id/tvctrl"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="200dp" />
<TextView
android:id="@+id/tvrslt"
android:layout_width="match_parent"
android:layout_height="57dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="132dp" />
</RelativeLayout>
Ini adalah hasil compilenya :
Seperti itulah penjelasan singkat mengenai cara membuat aplikasi kalkulator sederhana dengan menggunakan Android Studio. Semoga bermanfaat!