Eclipse Date Time Picker (EDTP)


EDTP:   SOLD       




In Android, picker is often used to set the date or time. See Figure 5.3.


Figure 5.3. Picker for setting the date 

1. Create another new project, with parameters like the following

| Project name     | Picker         |
|------------------|----------------|
| Build Target     | Android 2.2    |
| Application name | Membuat Picker |
| Package name     | com.picker     |
| Create Activity  | picker         |
| Min SDK version  | 8              |

2. First, we create the Main.xml layout section.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    android:text="@string/hello"
    android:id="@+id/dateAndTime"/>
  <Button android:text="Set the Date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/dayBtn"></Button>
  <Button android:text="Set the Time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/timeBtn"></Button>
</LinearLayout>

3. Next, we do a little coding in activity picker.java

package com.picker;
import java.text.DateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class picker extends Activity
    implements OnClickListener {

    DateFormat fmtDateAndTime =
    DateFormat.getDateTimeInstance();
 TextView dateAndTimeLabel;
 Calendar dateAndTime = Calendar.getInstance();
 DatePickerDialog.OnDateSetListener d =
 new DatePickerDialog.OnDateSetListener() {
 @Override
 public void onDateSet(DatePicker view, int year, int month,
 int day) {
 // TODO Auto-generated method stub
 dateAndTime.set(Calendar.YEAR, year);
 dateAndTime.set(Calendar.MONTH, month);
 dateAndTime.set(Calendar.DAY_OF_MONTH, day);
 updateLabel();
   }
};
 TimePickerDialog.OnTimeSetListener t =
 new TimePickerDialog.OnTimeSetListener() {
 @Override
 public void onTimeSet(TimePicker view, int jam, int menit) {
 // TODO Auto-generated method stub
 dateAndTime.set(Calendar.HOUR_OF_DAY, jam);
 dateAndTime.set(Calendar.MINUTE, menit);
 updateLabel();
 }
 };

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 Button dayBtn = (Button) findViewById(R.id.dayBtn);
 dayBtn.setOnClickListener(this);
 Button timeBtn = (Button) findViewById(R.id.timeBtn);
 timeBtn.setOnClickListener(this);
 dateAndTimeLabel = (TextView)
 findViewById(R.id.dateAndTime);
 updateLabel();
 }

 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 switch (v.getId()) {
 case R.id.dayBtn:
 settingTanggal();
 break;
 case R.id.timeBtn:
 setJam();
 break;
 }
 }

 private void updateLabel() {
 dateAndTimeLabel.setText(
 fmtDateAndTime.format(dateAndTime.getTime()));
 }

 private void settingTanggal() {
 new DatePickerDialog(picker.this, d,
 dateAndTime.get(Calendar.YEAR),
 dateAndTime.get(Calendar.MONTH),
 dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
 }

 private void setJam() {
 new TimePickerDialog(picker.this, t,
 dateAndTime.get(Calendar.HOUR_OF_DAY),
 dateAndTime.get(Calendar.MINUTE), true).show();
 }
}

Program Explanation

We start from the main.xml layout, in this section you prepare 2 buttons and a textview. The first button is given the id „dayBtn‟ (line 13) which will later be used to call the date picker, while the second button is given the id „timeBtn‟ (line 17) which will later be used to call the hour picker. Ok, very simple!

Now moving on to activity picker.java, You have to start concentrating. Hehehe.. Ok,

DatePickerDialog.onDateSetListener (line 24) plays a role in capturing information after the user has finished setting the time. At the same time, the onDataSet() method is called to update the Year, Month, Day, Date or Time, then displayed to the textview via the updateLabel() method.


Post a Comment

Previous Next

نموذج الاتصال