0% found this document useful (0 votes)
726 views2 pages

7 Picker

The document discusses how to create a date and time picker in Android. It involves designing an XML layout with two buttons and a text view. It then implements a Java activity class to handle button clicks and display the selected date and time. When the date button is clicked, a date picker dialog is shown to allow selecting the date. When the time button is clicked, a time picker dialog is shown. Listeners are used to capture the selections and update the text view with the combined date and time.

Uploaded by

priyojoko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
726 views2 pages

7 Picker

The document discusses how to create a date and time picker in Android. It involves designing an XML layout with two buttons and a text view. It then implements a Java activity class to handle button clicks and display the selected date and time. When the date button is clicked, a date picker dialog is shown to allow selecting the date. When the time button is clicked, a time picker dialog is shown. Listeners are used to capture the selections and update the text view with the combined date and time.

Uploaded by

priyojoko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Membuat PICKER

Dalam android, picker sering dimanfaatkan untuk melakukan setting


tanggal atau jam. Lihat disamping

1. Bikin project baru lagi, dengan parameter seperti berikut

2. Kita buat dulu bagian layout Main.xml

1: <?xml version="1.0" encoding="utf-8"?>


2: <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
3: android:orientation="vertical"
4: android:layout_width="fill_parent"
5: android:layout_height="fill_parent">
6: <TextView android:layout_width="fill_parent"
7: android:layout_height="wrap_content"
8: android:text="@string/hello"
9: android:id="@+id/dateAndTime"/>
10: <Button android:text="Set the Date"
11: android:layout_width="fill_parent"
12: android:layout_height="wrap_content"
13: android:id="@+id/dayBtn"></Button>
14: <Button android:text="Set the Time"
15: android:layout_width="fill_parent"
16: android:layout_height="wrap_content"
17: android:id="@+id/timeBtn"></Button>
18: </LinearLayout>

3. Berikutnya kita sedikt banyak koding di activity picker.java

1: package com.picker;
2:
3: import java.text.DateFormat;
4: import java.util.Calendar;
5:
6: import android.app.Activity;
7: import android.app.DatePickerDialog;
8: import android.app.TimePickerDialog;
9: import android.os.Bundle;
10: import android.view.View;
11: import android.view.View.OnClickListener;
12: import android.widget.Button;
13: import android.widget.DatePicker;
14: import android.widget.TextView;
15: import android.widget.TimePicker;
16:
17: public class picker extends Activity
18: implements OnClickListener {
19:
20: DateFormat fmtDateAndTime =
21: DateFormat.getDateTimeInstance();
22: TextView dateAndTimeLabel;
23: Calendar dateAndTime = Calendar.getInstance();
24: DatePickerDialog.OnDateSetListener d =
25: new DatePickerDialog.OnDateSetListener() {
26: @Override
27: public void onDateSet(DatePicker view, int year, int month,
28: int day) {
29: // TODO Auto-generated method stub
30: dateAndTime.set(Calendar.YEAR, year);
31: dateAndTime.set(Calendar.MONTH, month);
32: dateAndTime.set(Calendar.DAY_OF_MONTH, day);
33: updateLabel();
34: }
35: };
36: TimePickerDialog.OnTimeSetListener t =
37: new TimePickerDialog.OnTimeSetListener() {
38: @Override
39: public void onTimeSet(TimePicker view, int jam, int menit) {
40: // TODO Auto-generated method stub
41: dateAndTime.set(Calendar.HOUR_OF_DAY, jam);
42: dateAndTime.set(Calendar.MINUTE, menit);
43: updateLabel();
44: }
45: };
46:
47: /** Called when the activity is first created. */
48: @Override
49: public void onCreate(Bundle savedInstanceState) {
50: super.onCreate(savedInstanceState);
51: setContentView(R.layout.main);
52:
53: Button dayBtn = (Button) findViewById(R.id.dayBtn);
54: dayBtn.setOnClickListener(this);
55: Button timeBtn = (Button) findViewById(R.id.timeBtn);
56: timeBtn.setOnClickListener(this);
57: dateAndTimeLabel = (TextView)
58: findViewById(R.id.dateAndTime);
59: updateLabel();
60: }
61:
62: @Override
63: public void onClick(View v) {
64: // TODO Auto-generated method stub
65: switch (v.getId()) {
66: case R.id.dayBtn:
67: settingTanggal();
68: break;
69: case R.id.timeBtn:
70: setJam();
71: break;
72: }
73: }
74:
75: private void updateLabel() {
76: dateAndTimeLabel.setText(
77: fmtDateAndTime.format(dateAndTime.getTime()));
78: }
79:
80: private void settingTanggal() {
81: new DatePickerDialog(picker.this, d,
82: dateAndTime.get(Calendar.YEAR),
83: dateAndTime.get(Calendar.MONTH),
84: dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
85: }
86:
87: private void setJam() {
88: new TimePickerDialog(picker.this, t,
89: dateAndTime.get(Calendar.HOUR_OF_DAY),
90: dateAndTime.get(Calendar.MINUTE), true).show();
91: }
92: }

PENJELASAN PROGRAM
Kita mulai dari layout main.xml, di bagian ini kita menyiapkan 2 buah button dan sebuah textview.
Button pertama diberi id “dayBtn‟ (baris 13) nanti difungsikan untuk memanggil picker tanggal, sedangkan
button kedua diberi id “timeBtn‟ (baris 17) nanti dipakai untuk memanggil picker jam.
Sekarang lanjut ke activity picker.java, Date Picker Dialog.onDateSetListener (baris 24) berperan
menangkap informasi setelah user selesai melakukan setting waktu. Pada saat yang bersamaan,
method onDataSet() dipanggil untuk mengupdate Tahun, bulan, hari, tanggal maupun jam, kemudian
ditampilkan ke textview melalui method updateLabel().

You might also like