alert dialog ,notification dan intent...pertemuan ke -5 alert dialog ,notification dan intent alert...
TRANSCRIPT
Pertemuan ke -5
ALERT DIALOG ,NOTIFICATION DAN INTENT
ALERT DIALOG
1. Buat project android seperti biasa
2. Buat folder drawable dan siapkan 3 jenis gambar icon dengan ukuran 70 x 70 ke
dalam folder drawable dengan nama :
- save
- tick
- delete
Kemudian Taruh di dalam folder drawable
3. Isikan layout activity main seperti dibawah ini :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:src="@drawable/ic_launcher_background" />
<Button
android:id="@+id/btnAlert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dip"
android:padding="10dip"
android:text="Tampilkan Dialog" >
</Button>
<Button
android:id="@+id/btnAlertWithTwoBtns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="10dip"
android:text="Dialog dengan 2 Tombol" >
</Button>
<Button
android:id="@+id/btnAlertWithThreeBtns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="10dip"
android:text="Dialog dengan 3 Tombol" >
</Button>
<Button
android:id="@+id/btnCustomAlert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Custom Dialog" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dip"
android:gravity="center"
android:text="Pemrograman Perangkat Bergerak"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#58bc07"
android:textSize="17dp"
android:textStyle="bold" />
</LinearLayout>
4. Buat file layout baru di dalam folder layout yang berfungsi untuk menampilkan
detail info tentang aplikasi :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="Tentang Aplikasi "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#006699"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#006699"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="20dp"
android:text="Mobile Programming"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Android"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="20dp"
android:text="STIKI"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Mencoba Alert Dialog"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="30dp"
android:background="#CCFFCC"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_custom_alert_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
5. Buka Class MainActivity, kemudian isikan seperti dibawah ini (sesuaikan nama
class dengan nama MainActivity pada project anda) :
package com.example.chaulina.cthalert; import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivityAlert extends Activity implements OnClickListener {
Button btnAlertTwoBtns, btnCustomAlert, btnAlertWithThreeBtns, btnAlert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_alert);
btnAlertTwoBtns = (Button) findViewById(R.id.btnAlertWithTwoBtns);
btnCustomAlert = (Button) findViewById(R.id.btnCustomAlert);
btnAlertWithThreeBtns = (Button) findViewById (R.id.btnAlertWithThreeBtns);
btnAlert = (Button) findViewById (R.id.btnAlert);
btnAlertTwoBtns.setOnClickListener(this);
btnCustomAlert.setOnClickListener(this);
btnAlertWithThreeBtns.setOnClickListener(this);
btnAlert.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAlertWithTwoBtns:
// Creating
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(this);
// Setting dialog
alertDialog2.setTitle("Konfirmasi Hapus ... ?");
alertDialog2.setMessage("Yakin Data Dihapus ?");
alertDialog2.setIcon(R.drawable.delete);
alertDialog2.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Tombol YES ditekan", Toast.LENGTH_SHORT).show();
}
});
alertDialog2.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Tombol NO ditekan", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
alertDialog2.show();
break;
case R.id.btnAlert:
// Creating
AlertDialog.Builder alertDialog1= new AlertDialog.Builder(this);
// Setting Dialog
alertDialog1.setTitle("Alert Dialog");
alertDialog1.setMessage("Materi Dialog Mobile Programming");
alertDialog1.setIcon(R.drawable.tick);
alertDialog1.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Tombol OK ditekan", Toast.LENGTH_SHORT).show();
}
});
alertDialog1.show();
break;
case R.id.btnAlertWithThreeBtns:
// Creating
AlertDialog.Builder alertDialog3= new AlertDialog.Builder(this);
// Setting Dialog
alertDialog3.setTitle("Simpan File");
alertDialog3.setMessage("Simpan File Pekerjaan Anda");
alertDialog3.setIcon(R.drawable.save);
alertDialog3.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Tombol YES ditekan", Toast.LENGTH_SHORT).show();
}
});
alertDialog3.setNeutralButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Tombol NO ditekan", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
alertDialog3.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Tombol Cancel ditekan", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
alertDialog3.show();
break;
case R.id.btnCustomAlert:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_about);
Button dialobButton = (Button) dialog.findViewById(R.id.btn_custom_alert_ok);
dialobButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}}
6. Run project jika berhasil akan muncul tampilan seperti di
bawah ini
NOTIFICATION
1. Buat project android seperti biasa
2. Pada layout main isikan seperti di bawah ini :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/txtJudul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Judul"
android:inputType="textPersonName" />
<EditText
android:id="@+id/txtpesan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Pesan"
android:inputType="textPersonName" />
<Button
android:id="@+id/btnkirim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Kirim" />
</LinearLayout>
3. Buat MainActivity sesuaikan seperti di bawah ini :
package com.example.chaulina.cthnotif;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityNotif extends AppCompatActivity {
// deklarasi kode request
public static final int notifikasi = 1;
// deklarasi tombol
Button btnkirim;
// deklarasi edittext
EditText txtjudul, txtpesan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_notif);
// inisialisasi komponen
btnkirim = (Button) findViewById(R.id.btnkirim);
txtjudul = (EditText) findViewById(R.id.txtJudul);
txtpesan = (EditText) findViewById(R.id.txtpesan);
// aksi letika tombol diklik
btnkirim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// membuat komponen Inten
Intent intent = new Intent(getApplicationContext(), MainActivityNotif.class);
// memanggil method untuk menampilkan notifikasi
// dengan mengirimkan data yang dikirim dari komponen EditText
tampilNotifikasi(txtjudul.getText().toString()
, txtpesan.getText().toString(), intent);
}
});
}
private void tampilNotifikasi(String s, String s1, Intent intent) {
// membuat komponen pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivityNotif.this
, notifikasi, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// membuat komponen notifikasi
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivityNotif.this);
Notification notification;
notification = builder.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(s)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(MainActivityNotif.this.getResources()
, R.mipmap.ic_launcher))
.setContentText(s1)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) MainActivityNotif.this
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notifikasi, notification);
}
}
4. Run Program dan tampilkan hasil Notification yang dihasilkan
INTENT
1. Buat project android seperti biasa 2. Pada main layout isikan seperti berikut : <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:paddingTop="10dp">
<Button
android:id="@+id/btn_menu1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ini Menu1"
android:textColor="#fff"
android:background="@color/colorPrimaryDark"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:id="@+id/btn_menu2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ini Menu2"
android:background="@color/colorPrimary"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_marginBottom="166dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
3. Buat layout baru yang berfungsi untuk menampilkan intent ke 1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:paddingTop="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_nama_lengkap"
android:hint="Nama Lengkap"/>
<EditText
android:id="@+id/txt_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_nama_lengkap"
android:hint="alamat"/>
<EditText
android:id="@+id/txt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/txt_alamat"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="Email"/>
<EditText
android:id="@+id/txt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/txt_email"
android:ems="10"
android:inputType="phone"
android:hint="Phone"/>
<TextView
android:id="@+id/tv_kelamin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jenis Kelamin"
android:layout_marginTop="14dp"
android:layout_below="@+id/txt_phone"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<RadioButton
android:id="@+id/rLak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Laki-Laki"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_alignBaseline="@+id/tv_kelamin"
android:layout_alignBottom="@+id/tv_kelamin"
android:layout_toRightOf="@+id/tv_kelamin"
android:layout_toEndOf="@+id/tv_kelamin"/>
<RadioButton
android:id="@+id/rPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perempuan"
android:layout_alignBaseline="@+id/rLak"
android:layout_alignBottom="@+id/rLak"
android:layout_toRightOf="@+id/rLak"
android:layout_toEndOf="@+id/rLak"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"/>
<Button
android:id="@+id/bt_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:textColor="#fff"
android:background="@color/colorPrimaryDark"
android:layout_below="@+id/rLak"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp"/>
<Button
android:id="@+id/bt_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back"
android:background="@color/colorPrimary"
android:textColor="#fff"
android:layout_below="@+id/bt_register"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
3. Buat layout baru yang berfungsi untuk menampilkan intent ke 2
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical">
<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_background"
/>
</ScrollView>
</LinearLayout>
4. Buat Class baru untuk menjalankan prosess Intent1, sesuaikan isi seperti di bawah ini : package com.example.chaulina.cthintent; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Menu1 extends AppCompatActivity {
private Button mMenu1, mBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu1);
mMenu1 =(Button)findViewById(R.id.bt_register);
mBack =(Button)findViewById(R.id.bt_back);
mMenu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"latihan intent menu1",Toast.LENGTH_LONG).show();
}
});
mBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
Intent iBack = new Intent(getApplicationContext(), MainActivityCthInt.class);
startActivity(iBack);
finish();
5. Buat Class baru untuk menjalankan proses intent 2 , sesuaikan isi seperti di bawah ini :
package com.example.chaulina.cthintent; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Menu2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu2);
}
}
6. Pada MainActivity sesuaikan code seperti di bawah ini: package com.example.chaulina.cthintent; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivityCthInt extends AppCompatActivity {
private Button mBtn_menu1, mBtn_menu2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_cth_int);
//inisial tombol
mBtn_menu1 = (Button)findViewById(R.id.btn_menu1);
mBtn_menu2=(Button)findViewById(R.id.btn_menu2);
// function tombol
mBtn_menu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent iMenu1 = new Intent(getApplicationContext(),Menu1.class);
startActivity(iMenu1);
}
});
mBtn_menu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
Intent iMenu2 = new Intent(getApplicationContext(), Menu2.class);
startActivity(iMenu2);
7. Tugas Anda, ubahlah layout menu 2 menjadi tampilan halaman yang berisi foto dan nama anda serta beri tombol back untuk kembali ke menu utama