Pemprograman Mobile (Alert Widget) : Tugas Praktikum Bab Vi
Pemprograman Mobile (Alert Widget) : Tugas Praktikum Bab Vi
Pemprograman Mobile (Alert Widget) : Tugas Praktikum Bab Vi
BAB VI
Pemprograman Mobile
(Alert Widget)
DI SUSUN OLEH :
NURMAYULINA
H 131 1 6517
Pemprograman Mobile A
<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;
@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);
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();
}
}
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