-->
yrDJooVjUUVjPPmgydgdYJNMEAXQXw13gYAIRnOQ
Bookmark

Tutorial Membuat Biodata dan Memunculkannya dalam Bentuk Toast

Pada kesempatan ini kita akan membuat Laporan Biodata Mahasiswa dan memanggilnya dalam bentuk Toast atau bisa di sebut juga notifikasi, pada tutorial ini belum termasuk CRUD dan tidak memiliki data base seperti SQLite, Mysql atau Firebase, pada tutorial ini hanya pemanggilan biasa saja, ini hanya dalam bentuk teks yang di panggil dari sebuah edit Text atau sebuah menu pilih seperti spinner atau ChekBox dan di panggil dalam bentuk toast atau sebua notifikasi yang muncul pada bagian bawah dari sebuah aplikasi tersebut.

Apa Itu Toast ?

Toast merupakan sebuah mekanisme yang ada di Andorid untuk menampilkan popup kecil sebagai sebuah feedback dan akan menghilang setelah beberapa waktu. toast biasanya berada paling bawah

Berikut Langkah-Langkah -nya

1. Pada bagian Layout di bagian activity_main.xml,
tambahkan seperti di bawah
 
<androidx.constraintlayout.widget.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"
    android:padding="8dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText_Nim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Nim"
        android:inputType="number"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="8dp" />
    <EditText
        android:id="@+id/editText_NamaLengkap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Nama Lengkap"
        android:inputType="textPersonName"
        app:layout_constraintTop_toBottomOf="@+id/editText_Nim"
        tools:layout_editor_absoluteX="10dp" />
    <EditText
        android:id="@+id/editText_No_HP"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="No. Handpohne"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText_NamaLengkap" />
    <EditText
        android:id="@+id/editText_Alamat" android:layout_width="match_parent"
        android:layout_height="107dp"
        android:ems="10"
        android:gravity="start|top"
        android:hint="Alamat"
        android:inputType="textMultiLine|textPostalAddress"
        app:layout_constraintTop_toBottomOf="@+id/editText_No_HP"
        tools:layout_editor_absoluteX="5dp" />
    <TextView
        android:id="@+id/jk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:text="Jenis Kelamin :"
        android:textSize="17dp"
        app:layout_constraintEnd_toStartOf="@+id/radioGroup3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText_Alamat" />
    <RadioGroup
        android:id="@+id/radioGroup3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:orientation="horizontal"
        app:layout_constraintStart_toEndOf="@+id/jk"
        app:layout_constraintTop_toBottomOf="@+id/editText_Alamat">
        <RadioButton
            android:id="@+id/radioButton_L"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Laki-Laki" />
        <RadioButton
            android:id="@+id/radioButton_P"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Perempuan" />
    </RadioGroup>

    <TextView
        android:id="@+id/statu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:text="Status :"
        android:textSize="17dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/jk" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="4dp"
        android:text="Mahasiswa"
        app:layout_constraintStart_toEndOf="@+id/statu"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup3" />

    <TextView
        android:id="@+id/jur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        android:text="Jurusan :"
        android:textSize="17dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/statu" />
    <Spinner
        android:id="@+id/jurusan"
        android:layout_width="220dp"
        android:layout_height="30dp"
        android:layout_marginTop="12dp"
        app:layout_constraintStart_toStartOf="@+id/checkBox"
        app:layout_constraintTop_toBottomOf="@+id/checkBox" />

    <TextView
        android:id="@+id/angk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_weight="1"
        android:text="Angkatan :"
        android:textSize="17dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/jur" />
    <Spinner
        android:id="@+id/angkatan"
        android:layout_width="220dp"
        android:layout_height="30dp"
        android:layout_marginTop="12dp"
        app:layout_constraintStart_toStartOf="@+id/jurusan"
        app:layout_constraintTop_toBottomOf="@+id/jurusan" />
    <Button
        android:id="@+id/button"
        android:layout_width="217dp"
        android:layout_height="46dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="12dp"
        android:text="Next"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/angk" />
    <Button
        android:id="@+id/clear"
        android:layout_width="81dp"
        android:layout_height="46dp" android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="12dp"
        android:text="CLEAR"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/angkatan" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. Pada Java Bagian MainActivity.java, tambahkan sintax sperti berikut

package com.e.biodatamahasiswa;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    //createt.by|www.bertips.com
    String[] Jurusan = {"Teknik Informatika", "Sistem Informatika", "Farmasi.", "Ilmu Hukum", " Sastra & Bahasa Asing",
            "Ilmu Kelautan dan Perikanan", "Ilmu Sosial dan Ilmu Politik", "Sosiologi",
            "Kedokteran", " Kesehatan Masyarakat ", "Kedokteran Gigi", "Pendidikan Dokter Gigi", "Kehutanan", "Peternakan"};
    String[] Angkatan = {"2010", "2011", "2012", "2013", "2014","2015", "2016", "2017", "2018", "2019 ",
            "2020", "2021", "2022", "2023", "2024", "2025",
            "2026", "2027", "2028", "2029","2030"};
    Spinner jurusan, angkatan;
    Button button,clear;
    EditText editText_Nim, editText_NamaLengkap, editText_No_HP, editText_Alamat;
    RadioButton radioButton_L, radioButton_P;


    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioButton_L = findViewById(R.id.radioButton_L);
        radioButton_P = findViewById(R.id.radioButton_P);
        editText_Nim = findViewById(R.id.editText_Nim);
        editText_NamaLengkap = findViewById(R.id.editText_NamaLengkap);
        editText_No_HP = findViewById(R.id.editText_No_HP);
        editText_Alamat = findViewById(R.id.editText_Alamat);
        clear = findViewById(R.id.clear);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CLEAR();
            }
        });
        jurusan = findViewById(R.id.jurusan);
        jurusan.setOnItemSelectedListener(this);
        angkatan= findViewById(R.id.angkatan);
        angkatan.setOnItemSelectedListener(this);
        checkBox = findViewById(R.id.checkBox);
        ArrayAdapter jjurusan = new
                ArrayAdapter(this, android.R.layout.simple_spinner_item, Jurusan);

        jjurusan.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter aangkatan = new
                ArrayAdapter(this, android.R.layout.simple_spinner_item, Angkatan);

        aangkatan.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //Setting the ArrayAdapter data on the Spinner
        jurusan.setAdapter(jjurusan);
        angkatan.setAdapter(aangkatan);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String Nim = editText_Nim.getText().toString();
                String Nama_lengkap = editText_NamaLengkap.getText().toString();
                String No_Hp = editText_No_HP.getText().toString();
                String Alamat = editText_Alamat.getText().toString();
                String jk = (radioButton_L.isChecked()) ? " Laki - Laki" :
                        (radioButton_P.isChecked()) ? " Perempuan" : "";
                String status = "Status : \b";
                if (checkBox.isChecked()) {
                    status += "Mahasiswa";
                }
                String jur = String.valueOf(jurusan.getSelectedItem());
                String angk = String.valueOf(angkatan.getSelectedItem());
                String maessage = "NIM :\b" + Nim + "\n" +
                        "Nama Lengkap :\b" + Nama_lengkap + "\n" +
                        "No Handpohne :\b" + No_Hp + "\n" +
                        "Alamat :\b" + Alamat + "\n" +
                        "Jenis Kelamin :\b" + jk + "\n" +
                        status + "\n" +
                        "Jurusan :\b" + jur + "\n" +
                        "Angkatan :\b" + angk + "\n";
                Toast.makeText(MainActivity.this, maessage,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void onRadioButtonClicked(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        String msg = "\n";
        switch (view.getId()) {
            case R.id.radioButton_L:
                if (checked)
                    msg = "You Clicked Laki - Laki";
                break;
            case R.id.radioButton_P:
                if (checked)
                    msg = "You Clicked Perempuan ";
                break;
        }
        Toast.makeText(getApplicationContext(), msg,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long
            l) {
        Toast.makeText(getApplicationContext(), Jurusan[i],
                Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(), Angkatan[i],
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }
    private void CLEAR() {
        editText_Nim.setText("");
        editText_NamaLengkap.setText("");
        editText_No_HP.setText("");
        editText_Alamat.setText("");
        checkBox.setChecked(false);
        radioButton_L.setChecked(false);
        radioButton_P.setChecked(false);
    }
}
 /*
    Terimakasi Telah  Mampir di www.bertips.com
    tetap belajar salam coding teman-teman
     */

Posting Komentar

Posting Komentar

Silahkan berkomentar sesuai TOPIK diluar itu akan admin hapus komentar yang berbau spam,link aktif, judi dan pornografi dll.

Terimakasih.