Last active
March 19, 2019 06:53
-
-
Save adammb86/ca22701065e0f9a603210973bb83a24c to your computer and use it in GitHub Desktop.
HitungPersegi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:id="@+id/main_acticity" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_margin="@dimen/medium_dimen" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/tv_hitung_persegi" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/hitung_persegi" | |
android:textAlignment="center" | |
android:textSize="30sp" | |
android:textStyle="bold" /> | |
<EditText | |
android:id="@+id/edt_sisi" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="@dimen/medium_dimen" | |
android:layout_marginBottom="@dimen/small_dimen" | |
android:hint="@string/masukkan_sisi_persegi" | |
android:textSize="20sp" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/btn_hitung" | |
android:text="@string/hitung" | |
android:textAlignment="center" | |
android:textStyle="bold" | |
android:drawableLeft="@drawable/ic_flash_on_black_24dp" | |
android:layout_gravity="center" | |
android:layout_marginTop="@dimen/small_dimen" | |
android:layout_marginBottom="@dimen/medium_dimen"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Keliling: 0" | |
android:textSize="30sp" | |
android:layout_margin="@dimen/medium_dimen" | |
android:textStyle="bold" | |
android:layout_gravity="center" | |
android:id="@+id/tv_keliling" | |
android:textColor="@color/colorPrimary"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Luas: 0" | |
android:textSize="30sp" | |
android:textStyle="bold" | |
android:layout_gravity="center" | |
android:id="@+id/tv_luas" | |
android:textColor="@color/colorPrimary"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.hitungpersegi; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
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 implements View.OnClickListener { | |
EditText edtSisi; | |
Button btnHitung; | |
TextView tvKeliling, tvLuas; | |
double sisi, keliling, luas; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
edtSisi = findViewById(R.id.edt_sisi); | |
btnHitung = findViewById(R.id.btn_hitung); | |
tvKeliling = findViewById(R.id.tv_keliling); | |
tvLuas = findViewById(R.id.tv_luas); | |
btnHitung.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View view) { | |
if (view.getId() == R.id.btn_hitung) { | |
if (edtSisi.getText().toString() == null || edtSisi.getText().toString().isEmpty()) { | |
sisi = 0.0; | |
} else { | |
sisi = Double.parseDouble(edtSisi.getText().toString()); | |
} | |
keliling = 4 * sisi; | |
luas = sisi * sisi; | |
tvKeliling.setText("Keliling: " + keliling); | |
tvLuas.setText("Luas: " + luas); | |
} | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
luas = 0.0; | |
keliling = 0.0; | |
Toast.makeText(this, "Activity dalam kondisi onStart()", Toast.LENGTH_LONG).show(); | |
Log.d("onStart: inisialisasi nilai luas dan keliling serta memunculkan Toast", String.valueOf(luas)); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
Toast.makeText(this, "Activity ditutup.", Toast.LENGTH_LONG).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment