Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Nama : Eko Santoso

Kelas : 2021-C
NPM : 1412210029

PRAKTIKUM 3
ANDROID WIDGET

1. Coding
Main.Activity.java
package com.example.modul3_029;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class MainActivity extends AppCompatActivity
{

private TextView tvDisplayTglLahir;


private EditText etNPM, etNama;
private RadioGroup rbKelas, rbJenisKelamin;
private Spinner spAngkatan;
private LinearLayout cbBahasaPemrograman;
private Button btnSubmit, btnReset, btnTglLahir;
public String tglLahirValue = null;

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

etNPM = findViewById(R.id.et_npm);
etNama = findViewById(R.id.et_nama);
rbKelas = findViewById(R.id.rg_kelas);
rbJenisKelamin = findViewById(R.id.rg_jk);
spAngkatan = findViewById(R.id.sp_angkatan);
cbBahasaPemrograman =
findViewById(R.id.cb_bahasa_pemrograman);
btnSubmit = findViewById(R.id.btn_submit);
btnReset = findViewById(R.id.btn_reset);
btnTglLahir =
findViewById(R.id.btn_tgl_lahir);
tvDisplayTglLahir =
findViewById(R.id.tv_display_tgl_lahir);
btnTglLahir.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar =
Calendar.getInstance();
int year =
calendar.get(Calendar.YEAR);
int month =
calendar.get(Calendar.MONTH);
int dayOfMonth =
calendar.get(Calendar.DAY_OF_MONTH);

// Membuat DatePickerDialog untuk


memilih tanggal lahir
DatePickerDialog datePickerDialog =
new DatePickerDialog(MainActivity.this,
new
DatePickerDialog.OnDateSetListener() {
@Override
public void
onDateSet(DatePicker view, int year, int
monthOfYear, int dayOfMonth) {
// Mengonversi nilai
tanggal lahir menjadi string
String birthDate =
dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;

// Menampilkan nilai
tanggal lahir
tglLahirValue =
birthDate;
tvDisplayTglLahir.se
tText(tglLahirValue);
}
}, year, month, dayOfMonth);
// Menampilkan DatePickerDialog
datePickerDialog.show();
}
});

btnSubmit.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String npm =
etNPM.getText().toString();
String nama =
etNama.getText().toString();

int selectedKelasId =
rbKelas.getCheckedRadioButtonId();
RadioButton rbKelasSelected =
findViewById(selectedKelasId);
String kelas =
rbKelasSelected.getText().toString();

int selectedJenisKelaminId =
rbJenisKelamin.getCheckedRadioButtonId();
RadioButton rbJenisKelaminSelected =
findViewById(selectedJenisKelaminId);
String jenisKelamin =
rbJenisKelaminSelected.getText().toString();

String angkatan =
spAngkatan.getSelectedItem().toString();

// Mendapatkan jumlah checkbox


int checkboxCount =
cbBahasaPemrograman.getChildCount();
// Membuat list untuk menyimpan
hasil
ArrayList <String>
selectedCheckboxItem = new ArrayList<>();

// Melakukan iterasi pada semua


checkbox dan memeriksa statusnya
for (int i = 0; i < checkboxCount;
i++) {
View view =
cbBahasaPemrograman.getChildAt(i);

if (view instanceof CheckBox) {


CheckBox checkBox =
(CheckBox) view;
if (checkBox.isChecked()) {
// Jika checkbox
terpilih, tambahkan nilainya ke dalam list
selectedItems
selectedCheckboxItem.add
(checkBox.getText().toString());
}
}
}

Intent intent = new


Intent(MainActivity.this, SecondActivity.class);

intent.putExtra("npm", npm);
intent.putExtra("nama", nama);
intent.putExtra("kelas", kelas);
intent.putExtra("jenis_kelamin",
jenisKelamin);
intent.putExtra("angkatan",
angkatan);
intent.putExtra("tgl_lahir",
tglLahirValue);
intent.putStringArrayListExtra("baha
sa_pemrograman", selectedCheckboxItem);
startActivity(intent);
}
});
btnReset.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
etNPM.setText("");
etNama.setText("");
rbKelas.clearCheck();
rbJenisKelamin.clearCheck();
spAngkatan.setSelection(0);
tvDisplayTglLahir.setText("");
// Reset Checkbox adalah dengan
looping semua checkbox lalu reset statusnya
// Mendapatkan jumlah checkbox
int checkboxCount =
cbBahasaPemrograman.getChildCount();

// Melakukan iterasi pada semua


checkbox dan memeriksa statusnya
for (int i = 0; i < checkboxCount;
i++) {
View view =
cbBahasaPemrograman.getChildAt(i);

if (view instanceof CheckBox) {


CheckBox checkBox =
(CheckBox) view;
if (checkBox.isChecked()) {
checkBox.setChecked(fals
e);
}
}
}
}
});
}
}

Second.Activity.java
package com.example.modul3_029;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

import java.util.ArrayList;

public class SecondActivity extends


AppCompatActivity {

private TextView tvNPM, tvNama, tvKelas,


tvJenisKelamin, tvAngkatan, tvBahasaPemrograman,
tvTglLahir;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

tvNPM = findViewById(R.id.tv_npm);
tvNama = findViewById(R.id.tv_nama);
tvKelas = findViewById(R.id.tv_kelas);
tvJenisKelamin =
findViewById(R.id.tv_jenis_kelamin);
tvAngkatan = findViewById(R.id.tv_angkatan);
tvBahasaPemrograman =
findViewById(R.id.tv_bahasa_pemrograman);
tvTglLahir =
findViewById(R.id.tv_tgl_lahir);

String npm =
getIntent().getStringExtra("npm");
String nama =
getIntent().getStringExtra("nama");
String kelas =
getIntent().getStringExtra("kelas");
String jenisKelamin =
getIntent().getStringExtra("jenis_kelamin");
String angkatan =
getIntent().getStringExtra("angkatan");
String tgl_lahir =
getIntent().getStringExtra("tgl_lahir");
ArrayList<String> bahasa_pemrograman =
getIntent().getStringArrayListExtra("bahasa_pemrogra
man");

// Menggabungkan semua elemen dari ArrayList


menjadi satu string
StringBuilder sb = new StringBuilder();
for (String s : bahasa_pemrograman) {
sb.append(s).append("\n");
}

tvNPM.setText(npm);
tvNama.setText(nama);
tvKelas.setText(kelas);
tvJenisKelamin.setText(jenisKelamin);
tvJenisKelamin.setText(tgl_lahir);
tvAngkatan.setText(angkatan);
tvBahasaPemrograman.setText(sb.toString());
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/re
s/android">

<GridLayout
xmlns:android="http://schemas.android.com/apk/res/an
droid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Masukkan NPM"
android:textSize="15sp"
android:textStyle="bold" />

<EditText
android:id="@+id/et_npm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contoh: 1412210029"
android:inputType="number" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:text="Masukkan Nama"
android:textSize="15sp"
android:textStyle="bold" />

<EditText
android:id="@+id/et_nama"
android:layout_width="match_parent"
android:hint="Contoh: Eko Santoso"
android:layout_height="wrap_content" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:text="Kelas"
android:textSize="15sp"
android:textStyle="bold" />

<RadioGroup
android:id="@+id/rg_kelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rb_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />

<RadioButton
android:id="@+id/rb_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />

<RadioButton
android:id="@+id/rb_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
</RadioGroup>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:text="Angkatan"
android:textSize="15sp"
android:textStyle="bold" />

<Spinner
android:id="@+id/sp_angkatan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/string_angkatan"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:text="Jenis Kelamin"
android:textSize="15sp"
android:textStyle="bold" />

<RadioGroup
android:id="@+id/rg_jk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rb_laki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Laki-laki" />

<RadioButton
android:id="@+id/rb_perempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perempuan" />
</RadioGroup>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:text="Tanggal Lahir"
android:textSize="15sp"
android:textStyle="bold" />

<Button
android:id="@+id/btn_tgl_lahir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13dp"
android:text="Pilih"
android:focusable="false"
android:clickable="true"
/>

<TextView
android:id="@+id/tv_display_tgl_lahir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="silahkan pilih tanggal"
android:textSize="13sp"
android:textStyle="" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="8dp"
android:text="Bahasa Pemrograman
Keahlian"
android:textSize="15sp"
android:textStyle="bold" />

<LinearLayout
android:id="@+id/cb_bahasa_pemrograman"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<CheckBox
android:id="@+id/cb_cpp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++"/>
<CheckBox
android:id="@+id/cb_java"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Java"/>
<CheckBox
android:id="@+id/cb_python"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Python"/>
<CheckBox
android:id="@+id/cb_php"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="PHP"/>

</LinearLayout>

<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginRight="10dp"
android:text="Kirim" />
<Button
android:id="@+id/btn_reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Reset" />
</LinearLayout>

</GridLayout>
</ScrollView>

second_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/an
droid"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".SecondActivity">

<ImageView
android:layout_width="107dp"
android:layout_height="91dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Data Saved Successfully!"
android:layout_gravity="center"
android:textSize="15sp"
android:textStyle="bold" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="NPM : "
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_npm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="NPM" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginBottom="5dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Nama Lengkap : "
android:textSize="13sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_nama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Nama" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Kelas : "
android:textSize="13sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_kelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Kelas" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginBottom="5dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Angkatan : "
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_angkatan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Angkatan" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginBottom="5dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Tanggal Lahir : "
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_tgl_lahir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Tanggal Lahir" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginBottom="5dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Jenis Kelamin : "
android:textSize="13sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_jenis_kelamin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Jenis Kelamin" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginBottom="5dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Bahasa Pemrograman Keahlian :
"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_bahasa_pemrograman"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Bahasa Pemrograman
Keahlian" />

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginBottom="5dp" />

</LinearLayout>
2. GUI

Pada tampilan menu pertama ada Masukkan NPM yang inputnya berupa
number, lalu dibawahnya ada Masukkan Nama yang inputnya berupa text,
dibawahnya lagi ada Kelas dengan input Radio Button, setelah itu ada
Angkatan yang inputnya berupa Spinner, setelah itu ada Jenis Kelamin yang
inputnya juga berupa Radio Button, lalu ada Tanggal Lahir yang inputnya
Picker(DateTime), dibawahnya lagi ada Bahasa Pemrograman Keahlian yang
inputnya Chekbox, di button Kirim jika di klik akan masuk ke halaman kedua
yaitu menyimpan dan menampilkan hasil dari semua inputan tersebut, dan
jika button Reset di klik akan kembali mereset atau mengkosongkan yang
sudah di input.

You might also like