Tutorial Get Value dari Data Picker Android

hoy hoy ketemu lagi sama a’a ganteng nih :3 aka dananx dpapayas… Malem” ketimbang gak ngapa ngapain mending bagi bagi ilmu dong supaya tambah pinter dan memajukan nusa dan bangsa😀 nah sekarang saya ada sedikit ilmu nih dan mau ane share ke ente” semua.. ini request dari adik kelas gue.. sebagai kakak yang baik maka bagi bagi ilmu dan pengalaman (entah request nya bener ato gak, ane kira sih kayak gini😀 kalo salah panggil ane 3x (sial cowo panggilan -_-) kalo gak bener bisa tanyain ke gue lagi gpp anything for you dah #cieehh🙂 )

ya udah deh langsung aja ane praktekin cara nya :

1. Create New Project

2. Buat file main.xml

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:layout_width="fill_parent"  
4:    android:layout_height="fill_parent"  
5:    android:orientation="vertical" >  
6:    <Button  
7:      android:id="@+id/btnChangeDate"  
8:      android:layout_width="wrap_content"  
9:      android:layout_height="wrap_content"  
10:      android:text="Ganti Tanggal" />  
11:    <TextView  
12:      android:id="@+id/lblDate"  
13:      android:layout_width="wrap_content"  
14:      android:layout_height="wrap_content"  
15:      android:text="Tanggal Sekarang (M-D-YYYY): "  
16:      android:textAppearance="?android:attr/textAppearanceLarge" />  
17:    <TextView  
18:      android:id="@+id/tvDate"  
19:      android:layout_width="wrap_content"  
20:      android:layout_height="wrap_content"  
21:      android:text=""  
22:      android:textAppearance="?android:attr/textAppearanceLarge" />  
23:    <DatePicker  
24:      android:id="@+id/dpResult"  
25:      android:layout_width="wrap_content"  
26:      android:layout_height="wrap_content" />  
27:  </LinearLayout>  

3. Buat file java : DatePickerActivity.java

1:  package com.dananx.papayas;  
2:  import java.util.Calendar;  
3:  import android.app.Activity;  
4:  import android.app.DatePickerDialog;  
5:  import android.app.Dialog;  
6:  import android.os.Bundle;  
7:  import android.view.View;  
8:  import android.view.View.OnClickListener;  
9:  import android.widget.Button;  
10:  import android.widget.DatePicker;  
11:  import android.widget.TextView;  
12:  public class DatePickerActivity extends Activity {  
13:       private TextView tvDisplayDate;  
14:       private DatePicker dpResult;  
15:       private Button btnChangeDate;  
16:       private int year;  
17:       private int month;  
18:       private int day;  
19:       static final int DATE_DIALOG_ID = 999;  
20:       @Override  
21:       public void onCreate(Bundle savedInstanceState) {  
22:            super.onCreate(savedInstanceState);  
23:            setContentView(R.layout.main);  
24:            setCurrentDateOnView();  
25:            addListenerOnButton();  
26:       }  
27:       public void setCurrentDateOnView() {  
28:            tvDisplayDate = (TextView) findViewById(R.id.tvDate);  
29:            dpResult = (DatePicker) findViewById(R.id.dpResult);  
30:            final Calendar c = Calendar.getInstance();  
31:            year = c.get(Calendar.YEAR);  
32:            month = c.get(Calendar.MONTH);  
33:            day = c.get(Calendar.DAY_OF_MONTH);  
34:            tvDisplayDate.setText(new StringBuilder()  
35:                      .append(month + 1).append(" - ").append(day).append(" - ")  
36:                      .append(year).append(" "));  
37:            dpResult.init(year, month, day, null);  
38:       }  
39:       public void addListenerOnButton() {  
40:            btnChangeDate = (Button) findViewById(R.id.btnChangeDate);  
41:            btnChangeDate.setOnClickListener(new OnClickListener() {  
42:                 @Override  
43:                 public void onClick(View v) {  
44:                      showDialog(DATE_DIALOG_ID);  
45:                 }  
46:            });  
47:       }  
48:       @Override  
49:       protected Dialog onCreateDialog(int id) {  
50:            switch (id) {  
51:            case DATE_DIALOG_ID:  
52:                 return new DatePickerDialog(this, datePickerListener, year, month,  
53:                           day);  
54:            }  
55:            return null;  
56:       }  
57:       private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {  
58:            public void onDateSet(DatePicker view, int selectedYear,  
59:                      int selectedMonth, int selectedDay) {  
60:                 year = selectedYear;  
61:                 month = selectedMonth;  
62:                 day = selectedDay;  
63:                 tvDisplayDate.setText(new StringBuilder().append(month + 1)  
64:                           .append(" - ").append(day).append(" - ").append(year)  
65:                           .append(" "));  
66:                 dpResult.init(year, month, day, null);  
67:            }  
68:       };  
69:  }  

nah selesai dan jalankan aplikasi nya

nah ini adalah halaman pertama kita membuka aplikasi tersebut

kita akan coba mengganti tanggal nya

——

nah coba liat text view diatas “8 – 1 – 2013” itu sama seperti yang dipilih lewat datepicker tadi
nah ini yang dinamakan get value dari datepicker semoga sama seperti yang diharapkan yang request😀
semoga tutorial ini bermanfaat bagi nusa dan bangsa😀

cc : http://developer.android.com/guide/topics/ui/controls/pickers.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s