Cara Membuat Aplikasi Absen Mahasiswa dan Menghubungkan Antar Fragment
WELCOME TO MY BLOG
Disini saya akan membuat serta memberi tahu kamu cara menghubungkan antar Fragment, disini saya akan menghubungkan Fragment 1 dan Fragment 2, untuk menghubungkan antar Fragment 1 di Android Studio, kamu bisa mengikuti langkah-langkah berikut ini. Kita akan menggunakan FragmentManager dan FragmentTransaction untuk mengganti fragment berdasarkan interaksi pengguna.
Langkah-Langkah:
1. Membuat Proyek Android Studio Baru
- Buka Android Studio dan buat proyek baru.
- Pilih template "Empty Activity" karena kita akan menambahkan fragment secara manual nanti.
2. Membuat Fragment Baru
- Klik kanan pada folder
java→New→Fragment→Fragment (Blank). - Beri nama fragment kamu, misalnya
Fragment1 danFragment2. Lakukan ini untuk setiap ingin membuat Fragment baru.
Setiap fragment akan memiliki dua file:
- XML layout (
fragment1.xmldanfragment2.xml): Di sini kamu bisa mendesain tampilan dari setiap fragment. - Fragment Java/Kotlin class (
Fragment1.javadanFragment2.java): Ini adalah file logika untuk fragment tersebut.
3. MainActivity.java untuk Menghubungkan Antar Fragment
MainActivity.java.
package com.example.ppm_tugas;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button firstFragmentBtn, secondFragmentBtn, Fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstFragmentBtn = findViewById(R.id.fragment1btn);
secondFragmentBtn = findViewById(R.id.fragment2btn);
firstFragmentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new Fragment1());
}
});
secondFragmentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new Fragment2());
}
});
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,fragment);
fragmentTransaction.commit();
}
}
4. activity_main.xml untuk Layout
activity_main.xml, tambahkan elemen-elemen seperti judul, konten, dan gambar.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="600dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Pilih Absen warna biru hadir dan warna merah sakit atau izin"
android:textSize="20dp"
android:textColor="@color/black"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/fragment1btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:text="Biru"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="50dp"
/>
<Button
android:id="@+id/fragment2btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:text="Merah"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="50dp"
android:layout_marginEnd="50dp"
/>
</RelativeLayout>
5. Fragment1.java untuk Menghubungkan Antar Fragment
Fragment1.java.
package com.example.ppm_tugas;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class Fragment1 extends Fragment {
View view;
// Deklarasi komponen UI
private EditText etNama, etNim, etKelas, etMataKuliah;
private RadioButton radioButtonHadir;
private Button btnSimpan;
private TextView textViewKontak; // Deklarasi TextView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.activity_fragment1, container, false);
// Inisialisasi komponen UI
etNama = view.findViewById(R.id.etNama);
etNim = view.findViewById(R.id.etNim);
etKelas = view.findViewById(R.id.etKelas);
etMataKuliah = view.findViewById(R.id.etMataKuliah);
radioButtonHadir = view.findViewById(R.id.radioButtonHadir);
btnSimpan = view.findViewById(R.id.btnSimpan);
textViewKontak = view.findViewById(R.id.textViewKontak); // Inisialisasi TextView
// Set onClickListener untuk tombol "Simpan"
btnSimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Ambil input dari user
String nama = etNama.getText().toString();
String nim = etNim.getText().toString();
String kelas = etKelas.getText().toString();
String mataKuliah = etMataKuliah.getText().toString();
boolean hadir = radioButtonHadir.isChecked(); // misal ini untuk kehadiran
// Simpan ke SharedPreferences
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("DataMahasiswa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Nama", nama);
editor.putString("NIM", nim);
editor.putString("Kelas", kelas);
editor.putString("MataKuliah", mataKuliah);
editor.putBoolean("Hadir", hadir);
editor.apply();
// Tampilkan pesan bahwa data telah tersimpan
Toast.makeText(getActivity(), "Data Tersimpan", Toast.LENGTH_SHORT).show();
// Buat pesan untuk ditampilkan di textViewKontak
String message = "Nama: " + nama +
", NIM: " + nim +
", Kelas: " + kelas +
", Mata Kuliah: " + mataKuliah +
", Kehadiran: " + (hadir ?
"Hadir" : "Tidak Hadir");
textViewKontak.setText(message); // Set teks ke TextView
}
});
return view; // Pindahkan return ke akhir setelah logika selesai
}
}
6. activity_fragment1.xml untuk Layout
activity_fragment1.xml, tambahkan elemen-elemen seperti judul, konten, dan gambar.
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:background="#3498db"
tools:context=".Fragment1"
>
<EditText
android:id="@+id/etNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nama :"
android:textSize="20dp"
android:layout_marginTop="100dp"
/>
<EditText
android:id="@+id/etNim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nim :"
android:textSize="20dp"
android:layout_marginTop="130dp"
/>
<EditText
android:id="@+id/etKelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Kelas :"
android:textSize="20dp"
android:layout_marginTop="160dp"
/>
<EditText
android:id="@+id/etMataKuliah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Mata Kuliah :"
android:textSize="20dp"
android:layout_marginTop="190dp"
/>
<RadioButton
android:id="@+id/radioButtonHadir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Hadir"
android:layout_marginTop="220dp"
/>
<TextView
android:id="@+id/textViewKontak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100pt"
android:textAlignment="center"
android:textSize="10pt" />
<Button
android:id="@+id/btnSimpan"
android:layout_width="60pt"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Simpan"
android:layout_marginBottom="100dp"
android:layout_marginEnd="130dp"
/>
</RelativeLayout>
<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:orientation="vertical"
android:background="#3498db"
tools:context=".Fragment1"
>
<EditText
android:id="@+id/etNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nama :"
android:textSize="20dp"
android:layout_marginTop="100dp"
/>
<EditText
android:id="@+id/etNim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nim :"
android:textSize="20dp"
android:layout_marginTop="130dp"
/>
<EditText
android:id="@+id/etKelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Kelas :"
android:textSize="20dp"
android:layout_marginTop="160dp"
/>
<EditText
android:id="@+id/etMataKuliah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Mata Kuliah :"
android:textSize="20dp"
android:layout_marginTop="190dp"
/>
<RadioButton
android:id="@+id/radioButtonHadir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Hadir"
android:layout_marginTop="220dp"
/>
<TextView
android:id="@+id/textViewKontak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100pt"
android:textAlignment="center"
android:textSize="10pt" />
<Button
android:id="@+id/btnSimpan"
android:layout_width="60pt"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Simpan"
android:layout_marginBottom="100dp"
android:layout_marginEnd="130dp"
/>
</RelativeLayout>
7. Fragment2.java untuk Menghubungkan Antar Fragment
Fragment2.java.
package com.example.ppm_tugas;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class Fragment2 extends Fragment {
View view;
// Deklarasi komponen UI
private EditText etNama, etNim, etKelas, etMataKuliah;
private RadioButton radioButtonSakit, radioButtonIzin;
private Button btnSimpan;
private TextView textViewKontak; // Deklarasi TextView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.activity_fragment2, container, false);
// Inisialisasi komponen UI
etNama = view.findViewById(R.id.etNama);
etNim = view.findViewById(R.id.etNim);
etKelas = view.findViewById(R.id.etKelas);
etMataKuliah = view.findViewById(R.id.etMataKuliah);
radioButtonSakit = view.findViewById(R.id.radioButtonSakit); // Inisialisasi RadioButton Sakit
radioButtonIzin = view.findViewById(R.id.radioButtonIzin); // Inisialisasi RadioButton Izin
btnSimpan = view.findViewById(R.id.btnSimpan);
textViewKontak = view.findViewById(R.id.textViewKontak); // Inisialisasi TextView
// Set onClickListener untuk tombol "Simpan"
btnSimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Ambil input dari user
String nama = etNama.getText().toString();
String nim = etNim.getText().toString();
String kelas = etKelas.getText().toString();
String mataKuliah = etMataKuliah.getText().toString();
// Tentukan status kehadiran
String statusKehadiran = "";
if (radioButtonSakit.isChecked()) {
statusKehadiran = "Sakit";
} else if (radioButtonIzin.isChecked()) {
statusKehadiran = "Izin";
}
// Simpan ke SharedPreferences
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("DataMahasiswa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Nama", nama);
editor.putString("NIM", nim);
editor.putString("Kelas", kelas);
editor.putString("MataKuliah", mataKuliah);
editor.putString("StatusKehadiran", statusKehadiran);
editor.apply();
// Tampilkan pesan bahwa data telah tersimpan
Toast.makeText(getActivity(), "Data Tersimpan", Toast.LENGTH_SHORT).show();
// Buat pesan untuk ditampilkan di textViewKontak
String message = "Nama: " + nama +
", NIM: " + nim +
", Kelas: " + kelas +
", Mata Kuliah: " + mataKuliah +
", Kehadiran: " + statusKehadiran;
textViewKontak.setText(message); // Set teks ke TextView
}
});
return view; // Pindahkan return ke akhir setelah logika selesai
}
}
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class Fragment2 extends Fragment {
View view;
// Deklarasi komponen UI
private EditText etNama, etNim, etKelas, etMataKuliah;
private RadioButton radioButtonSakit, radioButtonIzin;
private Button btnSimpan;
private TextView textViewKontak; // Deklarasi TextView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.activity_fragment2, container, false);
// Inisialisasi komponen UI
etNama = view.findViewById(R.id.etNama);
etNim = view.findViewById(R.id.etNim);
etKelas = view.findViewById(R.id.etKelas);
etMataKuliah = view.findViewById(R.id.etMataKuliah);
radioButtonSakit = view.findViewById(R.id.radioButtonSakit); // Inisialisasi RadioButton Sakit
radioButtonIzin = view.findViewById(R.id.radioButtonIzin); // Inisialisasi RadioButton Izin
btnSimpan = view.findViewById(R.id.btnSimpan);
textViewKontak = view.findViewById(R.id.textViewKontak); // Inisialisasi TextView
// Set onClickListener untuk tombol "Simpan"
btnSimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Ambil input dari user
String nama = etNama.getText().toString();
String nim = etNim.getText().toString();
String kelas = etKelas.getText().toString();
String mataKuliah = etMataKuliah.getText().toString();
// Tentukan status kehadiran
String statusKehadiran = "";
if (radioButtonSakit.isChecked()) {
statusKehadiran = "Sakit";
} else if (radioButtonIzin.isChecked()) {
statusKehadiran = "Izin";
}
// Simpan ke SharedPreferences
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("DataMahasiswa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Nama", nama);
editor.putString("NIM", nim);
editor.putString("Kelas", kelas);
editor.putString("MataKuliah", mataKuliah);
editor.putString("StatusKehadiran", statusKehadiran);
editor.apply();
// Tampilkan pesan bahwa data telah tersimpan
Toast.makeText(getActivity(), "Data Tersimpan", Toast.LENGTH_SHORT).show();
// Buat pesan untuk ditampilkan di textViewKontak
String message = "Nama: " + nama +
", NIM: " + nim +
", Kelas: " + kelas +
", Mata Kuliah: " + mataKuliah +
", Kehadiran: " + statusKehadiran;
textViewKontak.setText(message); // Set teks ke TextView
}
});
return view; // Pindahkan return ke akhir setelah logika selesai
}
}
8. activity_fragment2.xml untuk Layout
activity_fragment2.xml, tambahkan elemen-elemen seperti judul, konten, dan gambar.
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Fragment2"
android:orientation="vertical"
android:background="#D30000"
>
<EditText
android:id="@+id/etNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nama :"
android:textSize="20dp"
android:layout_marginTop="100dp"
/>
<EditText
android:id="@+id/etNim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nim :"
android:textSize="20dp"
android:layout_marginTop="130dp"
/>
<EditText
android:id="@+id/etKelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Kelas :"
android:textSize="20dp"
android:layout_marginTop="160dp"
/>
<EditText
android:id="@+id/etMataKuliah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Mata Kuliah :"
android:textSize="20dp"
android:layout_marginTop="190dp"
/>
<RadioButton
android:id="@+id/radioButtonSakit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Sakit"
android:layout_marginTop="220dp"
/>
<RadioButton
android:id="@+id/radioButtonIzin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Izin"
android:layout_marginTop="250dp"
/>
<TextView
android:id="@+id/textViewKontak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100pt"
android:textAlignment="center"
android:textSize="10pt" />
<Button
android:id="@+id/btnSimpan"
android:layout_width="60pt"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Simpan"
android:layout_marginBottom="100dp"
android:layout_marginEnd="130dp"
/>
</RelativeLayout>
<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"
tools:context=".Fragment2"
android:orientation="vertical"
android:background="#D30000"
>
<EditText
android:id="@+id/etNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nama :"
android:textSize="20dp"
android:layout_marginTop="100dp"
/>
<EditText
android:id="@+id/etNim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Nim :"
android:textSize="20dp"
android:layout_marginTop="130dp"
/>
<EditText
android:id="@+id/etKelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Kelas :"
android:textSize="20dp"
android:layout_marginTop="160dp"
/>
<EditText
android:id="@+id/etMataKuliah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukan Mata Kuliah :"
android:textSize="20dp"
android:layout_marginTop="190dp"
/>
<RadioButton
android:id="@+id/radioButtonSakit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Sakit"
android:layout_marginTop="220dp"
/>
<RadioButton
android:id="@+id/radioButtonIzin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Izin"
android:layout_marginTop="250dp"
/>
<TextView
android:id="@+id/textViewKontak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100pt"
android:textAlignment="center"
android:textSize="10pt" />
<Button
android:id="@+id/btnSimpan"
android:layout_width="60pt"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Simpan"
android:layout_marginBottom="100dp"
android:layout_marginEnd="130dp"
/>
</RelativeLayout>
9. Menjalankan Aplikasi
Setelah semuanya diatur, jalankan aplikasi untuk melihat bagaimana fragment saling terhubung.
Aplikasi ini dibuat untuk absensi mahasiswa, yang bertujuan untuk mempermudah proses absensi.
Dengan mengikuti langkah-langkah ini, kamu dapat membuat aplikasi Android yang menampilkan dan menghubungkan beberapa fragment. Kamu juga bisa menambahkan fitur lain, seperti menyimpan data menggunakan database lokal (SQLite atau Room) atau menampilkan data dari API online.




















Komentar
Posting Komentar