Pemprograman Mobile (Alert Widget) : Tugas Praktikum Bab Vi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

TUGAS PRAKTIKUM

BAB VI

Pemprograman Mobile
(Alert Widget)

DI SUSUN OLEH :

NURMAYULINA

H 131 1 6517

Pemprograman Mobile A

PROGRAM STUDI ILMU KOMPUTER


DEPARTEMEN MATEMATIKA
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS HASANUDDIN
2019
Sintaks pada “ activity_main.xml “ sebagai berikut :
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color2"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="ALERT WIDGET"
android:textAlignment="center"
android:textColor="@color/color1"
android:textSize="20dp"
android:textStyle="bold" />

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/icon" />

<EditText
android:id="@+id/nim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="NIM"
android:inputType="textPersonName"
android:paddingLeft="30dp"/>

</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/icon" />

<EditText
android:id="@+id/nama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="NAMA"
android:inputType="textPersonName"
android:paddingLeft="30dp"/>
</FrameLayout>

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">

<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pria" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Wanita" />
</RadioGroup>

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/color2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="540dp"
android:orientation="horizontal"
android:layout_marginTop="200dp"
android:padding="5dp">

<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="CANCEL" />

<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:text="OK" />

</LinearLayout>

</LinearLayout>

</LinearLayout>
Sintaks pada “ MainActivity.java “ sebagai berikut :
package com.example.alertwidget;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements


OnClickListener {
Button buttonCancel, buttonOK;
EditText nama, nim;
String txt1, txt2, txt3, txt4;
RadioButton radioButton;
RadioGroup radioGroup;
Spinner agama;

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

nim = findViewById(R.id.nim);
nama = findViewById(R.id.nama);

radioGroup = findViewById(R.id.radioGroup);

buttonOK = findViewById(R.id.okButton);
buttonOK.setOnClickListener(this);
buttonCancel = findViewById(R.id.cancelButton);
buttonCancel.setOnClickListener(this);

agama = findViewById(R.id.spinner);

List<String> categories = new ArrayList<String>();


categories.add("ISLAM");
categories.add("KRISTEN");
categories.add("KATOLIK");
categories.add("HINDU");
categories.add("BUDDHA");
categories.add("KONG HU CU");

ArrayAdapter<String> dataAdapter = new


ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
categories);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dr
opdown_item);
agama.setAdapter(dataAdapter);
}

@Override
public void onBackPressed() {
new
AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are You Sure Want To Exit ?")
.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}

@Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(selectedId);

switch (v.getId()) {
case R.id.cancelButton:
nim.setText("");
nama.setText("");
break;
case R.id.okButton:
txt1 = nim.getText().toString();
txt2 = nama.getText().toString();
txt3 = radioButton.getText().toString();
txt4 = agama.getSelectedItem().toString();
AlertDialog.Builder ad = new
AlertDialog.Builder(this);
ad.setIcon(android.R.drawable.ic_dialog_info);
ad.setTitle("Data Anda");
ad.setMessage("NIM : " + txt1 + "\nNama : " + txt2 +
"\nJenis Kelamin : " + txt3 + "\nAgama : " + txt4);
ad.show();
break;
}
}

@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "Apps Minimized",
Toast.LENGTH_SHORT).show();
}
}

Hasil Run nya sebagai berikut :

Mengisi
Form NIM
dan Nama
Memilih opsi
RadioGroup
jenis kelamin
antara
RadioButton
Pria atau
wanita

Memilih opsi
pilihan
agama
dalam
pilihan
spinner
Tekan
Buttom “OK”
dan akan
ditampilkan
info data
yang telah
diisi.

Setelah menekan
tombol kembali pada
android anda akan
ditampilkan menu exit.
Setelah keluar akan
diberi informasi bahwa
aplikasi ditutup atau
diminimized

You might also like