Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- //definisi widget
- EditText etNama, etNPM, etAlamat;
- Spinner spProdi;
- CheckBox cbxTeknologi, cbxKuliner;
- RadioGroup rgDomisili;
- RadioButton rbDomisili;
- Button btToast, btNotifikasi, btDialog, btKeluar, btDetail, btSnack;
- @Override
- protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //hubungkan XML dengan java
- etNama = (EditText) findViewById(R.id.et_nama);
- etNPM = (EditText) findViewById(R.id.et_npm);
- etAlamat = (EditText) findViewById(R.id.et_alamat);
- spProdi = (Spinner) findViewById(R.id.sp_prodi);
- cbxTeknologi = (CheckBox) findViewById(R.id.cb_teknologi);
- cbxKuliner = (CheckBox) findViewById(R.id.cb_kuliner);
- rgDomisili = (RadioGroup) findViewById(R.id.rg_domisili);
- btToast = (Button) findViewById(R.id.bt_toast);
- btDialog = (Button) findViewById(R.id.bt_dialog);
- btNotifikasi = (Button) findViewById(R.id.bt_notif);
- btKeluar = (Button) findViewById(R.id.bt_keluar);
- btDetail = (Button) findViewById(R.id.bt_detil);
- btSnack = (Button) findViewById(R.id.bt_snack);
- //memberikan fungsi pada tombol
- btToast.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(getApplicationContext(), "Hai kawan, apa kabar?", Toast.LENGTH_SHORT).show();
- }
- });
- btDialog.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //menampilkan dialog
- new AlertDialog.Builder(MainActivity.this)
- .setIcon(R.mipmap.ic_launcher)
- .setTitle("Perhatian")
- .setMessage("Ini dialog box")
- .setCancelable(true)
- .setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(),
- "Anda baru menekan tombol OK",
- Toast.LENGTH_SHORT)
- .show();
- }
- })
- .setNegativeButton("Batal", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(),
- "Tombol Batal ditekan",
- Toast.LENGTH_SHORT)
- .show();
- }
- })
- .setNeutralButton("Netral", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- Toast.makeText(getApplicationContext(),
- "Tombol Netral ditekan",
- Toast.LENGTH_SHORT)
- .show();
- }
- }).show();
- }
- });
- btNotifikasi.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //notifikasi
- NotificationCompat.Builder mBuilder =
- new NotificationCompat.Builder(getApplicationContext(), "notify_001");
- Intent ii = new Intent(getApplicationContext(), MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, ii, 0);
- NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
- bigText.setBigContentTitle("Latihan01");
- bigText.setSummaryText("Ini adalah notifikasi dariku");
- mBuilder.setContentIntent(pendingIntent);
- mBuilder.setSmallIcon(R.mipmap.ic_launcher);
- mBuilder.setContentTitle("Aplikasi buatanku");
- mBuilder.setContentText("Notifikasi ini menggunakan java");
- mBuilder.setPriority(Notification.PRIORITY_MAX);
- mBuilder.setStyle(bigText);
- mBuilder.setDefaults(Notification.DEFAULT_SOUND); //suara
- mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000, 1000}); //getar
- NotificationManager mNotificationManager =
- (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- NotificationChannel channel = new NotificationChannel("notify_001",
- "channelku",
- NotificationManager.IMPORTANCE_DEFAULT);
- mNotificationManager.createNotificationChannel(channel);
- }
- mNotificationManager.notify(0, mBuilder.build());
- }
- });
- btKeluar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish(); //tutup aplikasi
- }
- });
- btDetail.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(getApplicationContext(),
- "Belum dibuat!",
- Toast.LENGTH_SHORT)
- .show();
- }
- });
- btSnack.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- View v = findViewById(R.id.main_layout_id);
- String message = "Snackbar message";
- int duration = Snackbar.LENGTH_SHORT;
- Snackbar.make(v, message, duration).show();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement