Sei sulla pagina 1di 7

File

MainActivity.jav
package com.example.udien.iqroudin;

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 angka_pertama, angka_kedua;
Button tambah, kurang, kali, bagi, bersihkan;
TextView hasil;
int angka1,angka2;

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

angka_pertama = (EditText) findViewById(R.id.editText);


angka_kedua = (EditText) findViewById(R.id.editText2);

tambah = (Button)findViewById(R.id.btntbh);
kurang = (Button)findViewById(R.id.btnkrg);
kali = (Button)findViewById(R.id.btnkli);
bagi = (Button)findViewById(R.id.btnbgi);
bersihkan = (Button) findViewById(R.id.btnhps);

hasil = (TextView) findViewById(R.id.textView3);

tambah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((angka_pertama.getText().length()>0) &&
(angka_kedua.getText().length()>0))
{
double angka1 =
Double.parseDouble(angka_pertama.getText().toString());
double angka2 =
Double.parseDouble(angka_kedua.getText().toString());
double result = angka1 + angka2;
hasil.setText(Double.toString(result));
}
else {
Toast toast = Toast.makeText(MainActivity.this, "Mohon masukkan
Angka pertama & Kedua", Toast.LENGTH_LONG);
toast.show();
}

}
});

kali.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((angka_pertama.getText().length()>0) &&
(angka_kedua.getText().length()>0))
{
double angka1 =
Double.parseDouble(angka_pertama.getText().toString());
double angka2 =
Double.parseDouble(angka_kedua.getText().toString());
double result = angka1 * angka2;
hasil.setText(Double.toString(result));

}
else {
Toast toast = Toast.makeText(MainActivity.this, "Mohon masukkan
Angka pertama & Kedua", Toast.LENGTH_LONG);
toast.show();
}
}
});

kurang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((angka_pertama.getText().length()>0) &&
(angka_kedua.getText().length()>0))
{
double angka1 =
Double.parseDouble(angka_pertama.getText().toString());
double angka2 =
Double.parseDouble(angka_kedua.getText().toString());
double result = angka1 - angka2;
hasil.setText(Double.toString(result));
}
else {
Toast toast = Toast.makeText(MainActivity.this, "Mohon masukkan
Angka pertama & Kedua", Toast.LENGTH_LONG);
toast.show();
}
}
});

bagi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((angka_pertama.getText().length()>0) &&
(angka_kedua.getText().length()>0))
{
double angka1 =
Double.parseDouble(angka_pertama.getText().toString());
double angka2 =
Double.parseDouble(angka_kedua.getText().toString());
double result = angka1 / angka2;
hasil.setText(Double.toString(result));
}
else {
Toast toast = Toast.makeText(MainActivity.this, "Mohon masukkan
Angka pertama & Kedua", Toast.LENGTH_LONG);
toast.show();
}
}
});

bersihkan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
angka_pertama.setText("");
angka_kedua.setText("");
hasil.setText("");
angka_pertama.requestFocus();
}

});
}
}

Activity_main.xml

Text
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_editor_absoluteY="81dp">

<ImageView
android:id="@+id/imageView"
android:layout_width="230dp"
android:layout_height="184dp"
android:layout_marginStart="66dp"
android:layout_marginTop="3dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="34dp"
android:contentDescription="TODO"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside"
app:srcCompat="@drawable/iq" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtwel"
app:layout_constraintBottom_toTopOf="@+id/editText"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />

<EditText
android:id="@+id/editText"
android:layout_width="259dp"
android:layout_height="36dp"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="66dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="103dp"
android:ems="10"
android:hint="Masukkan angka pertama"
android:inputType="number"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@+id/editText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText
android:id="@+id/editText2"
android:layout_width="259dp"
android:layout_height="45dp"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="44dp"
android:layout_marginEnd="81dp"
android:layout_marginBottom="24dp"
android:ems="10"
android:hint="Masukkan angka kedua"
android:inputType="number"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@+id/btntbh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
android:id="@+id/btnkrg"
android:layout_width="80dp"
android:layout_height="54dp"
android:layout_alignBaseline="@+id/btntbh"
android:layout_alignBottom="@+id/btntbh"
android:layout_marginStart="7dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="112dp"
android:layout_toEndOf="@+id/btntbh"
android:layout_toRightOf="@+id/btntbh"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnbgi"
app:layout_constraintStart_toEndOf="@+id/btnkli"
app:layout_constraintTop_toBottomOf="@+id/editText2" />

<Button
android:id="@+id/btnkli"
android:layout_width="78dp"
android:layout_height="54dp"
android:layout_alignBaseline="@+id/btnbgi"
android:layout_alignBottom="@+id/btnbgi"
android:layout_marginStart="16dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="21dp"
android:layout_marginBottom="112dp"
android:layout_toStartOf="@+id/btnbgi"
android:layout_toLeftOf="@+id/btnbgi"
android:text="x"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnkrg"
app:layout_constraintStart_toEndOf="@+id/btntbh"
app:layout_constraintTop_toBottomOf="@+id/editText2" />

<Button
android:id="@+id/btnbgi"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_above="@+id/btnhps"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="6dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="19dp"
android:layout_marginBottom="16dp"
android:text="/"
app:layout_constraintBottom_toTopOf="@+id/btnhps"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnkrg"
app:layout_constraintTop_toTopOf="@+id/textView"
app:layout_constraintVertical_bias="1.0" />

<Button
android:id="@+id/btntbh"
android:layout_width="75dp"
android:layout_height="55dp"
android:layout_below="@+id/editText2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="26dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="7dp"
android:text="+"
app:layout_constraintBottom_toTopOf="@+id/txtHsl"
app:layout_constraintEnd_toStartOf="@+id/btnkli"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />

<Button
android:id="@+id/btnhps"
android:layout_width="112dp"
android:layout_height="76dp"
android:layout_below="@+id/btntbh"
android:layout_centerHorizontal="true"
android:layout_marginStart="281dp"
android:layout_marginTop="17dp"
android:layout_marginBottom="25dp"
android:text="clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnkrg"
app:layout_constraintVertical_bias="0.0" />

<TextView
android:id="@+id/txtHsl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnhps"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="20dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="306dp"
android:layout_marginBottom="91dp"
android:text="HASIL"
android:textAlignment="center"
android:textStyle="normal|bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btntbh"
app:layout_constraintVertical_bias="0.243" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtHsl"
android:layout_centerHorizontal="true"
android:layout_marginStart="21dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="202dp"
android:layout_marginBottom="41dp"
android:textAlignment="center"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnhps"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtHsl" />

</android.support.constraint.ConstraintLayout>
HASIL RUNING APLIKASI