{"id":1515,"date":"2019-02-27T16:20:28","date_gmt":"2019-02-27T16:20:28","guid":{"rendered":"https:\/\/codewk.com\/?p=1515"},"modified":"2019-04-05T11:41:35","modified_gmt":"2019-04-05T11:41:35","slug":"android-date-time-picker-kullanimi","status":"publish","type":"post","link":"https:\/\/codewk.com\/android-date-time-picker-kullanimi\/","title":{"rendered":"Android – Date Picker & Time Picker Kullan\u0131m\u0131"},"content":{"rendered":"\n

Merhaba arkada\u015flar, bu yaz\u0131da Android uygulamalarda kullan\u0131lan Date Picker ve Time Picker nedir, nas\u0131l kullan\u0131l\u0131r bunlar\u0131 \u00f6\u011frenece\u011fiz ve bununla ilgili Button ve TextView’lerden olu\u015fan bir uygulama yapaca\u011f\u0131z. Gerek web uygulamalarda, gerekse mobil uygulamalarda kullan\u0131lan zaman kavram\u0131 \u00e7ok \u00f6nemlidir. \u015eimdir ise Date Picker ve Time Picker’\u0131n ne oldu\u011funu anlamaya \u00e7al\u0131\u015fal\u0131m. Ve daha sonras\u0131nda da basit bir uygulama ile olay\u0131 kavramaya \u00e7al\u0131\u015fal\u0131m. Ba\u015fl\u0131yoruz.. ????<\/p>\n\n\n\n

Date Picker :<\/strong> Anlam\u0131 “tarih se\u00e7ici<\/em>“dir. Tarih belirlemede kullan\u0131l\u0131r.
Time Picker :<\/strong> Anlam\u0131 “saat se\u00e7ici<\/em>“dir. Saat belirlemede kullan\u0131l\u0131r.
Bu iki yap\u0131 kullan\u0131c\u0131lar\u0131n manuel olarak saat ve tarih belirlemesine gerek kalmadan basit bir diyalog penceresi kullanmas\u0131n\u0131 sa\u011flar.<\/p>\n\n\n\n

\u00d6rnek Uygulama :<\/h2><\/strong><\/p>\n\n\n\n

Bu uygulamada giri\u015f ekran\u0131nda “Saat” ve “Tarih” ad\u0131nda iki buton olacak ve kullan\u0131c\u0131lar a\u00e7\u0131lan diyalog penceresinden saat ve tarihi se\u00e7ebilecekler ve biz de bu iki yap\u0131n\u0131n kullan\u0131m\u0131n\u0131 anlam\u0131\u015f olaca\u011f\u0131z. Hadi ba\u015flayal\u0131m !<\/p>\n\n\n\n

activity_main.xml<\/h2><\/font>\n\n\n\n
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"
xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"
xmlns:tools=\"http:\/\/schemas.android.com\/tools\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:background=\"#e0e0e0\"
tools:context=\".MainActivity\"
tools:layout_editor_absoluteY=\"25dp\">

<Button
android:id=\"@+id\/button\"
android:layout_width=\"115dp\"
android:layout_height=\"60dp\"
android:layout_marginEnd=\"20dp\"
android:layout_marginLeft=\"68dp\"
android:layout_marginRight=\"20dp\"
android:layout_marginStart=\"68dp\"
android:background=\"#463c48\"
android:text=\"SAAT\"
app:layout_constraintBaseline_toBaselineOf=\"@+id\/button2\"
app:layout_constraintEnd_toStartOf=\"@+id\/button2\"
app:layout_constraintStart_toStartOf=\"parent\" \/>

<Button
android:id=\"@+id\/button2\"
android:layout_width=\"115dp\"
android:layout_height=\"60dp\"
android:layout_marginBottom=\"71dp\"
android:layout_marginEnd=\"68dp\"
android:layout_marginRight=\"68dp\"
android:background=\"#463c48\"
android:text=\"TAR\u0130H\"
app:layout_constraintBottom_toTopOf=\"@+id\/textView2\"
app:layout_constraintEnd_toEndOf=\"parent\"
app:layout_constraintStart_toEndOf=\"@+id\/button\"
app:layout_constraintTop_toBottomOf=\"@+id\/textView\" \/>

<TextView
android:id=\"@+id\/textView\"
android:layout_width=\"wrap_content\"
android:layout_height=\"0dp\"
android:layout_marginBottom=\"75dp\"
android:layout_marginLeft=\"8dp\"
android:layout_marginStart=\"8dp\"
android:layout_marginTop=\"128dp\"
android:text=\" Tarih ve saati se\u00e7mek i\u00e7in butonlara t\u0131klay\u0131n\u0131z.\"
android:textColor=\"#000000\"
android:textSize=\"18dp\"
app:layout_constraintBottom_toTopOf=\"@+id\/button2\"
app:layout_constraintStart_toStartOf=\"parent\"
app:layout_constraintTop_toTopOf=\"parent\" \/>

<TextView
android:id=\"@+id\/textView2\"
android:layout_width=\"wrap_content\"
android:layout_height=\"30dp\"
android:layout_marginBottom=\"23dp\"
android:paddingLeft=\"10dp\"
android:textColor=\"#000000\"
android:textSize=\"20dp\"
android:textStyle=\"bold\"
app:layout_constraintBottom_toTopOf=\"@+id\/textView3\"
app:layout_constraintEnd_toEndOf=\"parent\"
app:layout_constraintStart_toStartOf=\"parent\"
app:layout_constraintTop_toBottomOf=\"@+id\/button2\" \/>

<TextView
android:id=\"@+id\/textView3\"
android:layout_width=\"wrap_content\"
android:layout_height=\"30dp\"
android:layout_marginBottom=\"92dp\"
android:paddingRight=\"10dp\"
android:textColor=\"#000000\"
android:textSize=\"20dp\"
android:textStyle=\"bold\"
app:layout_constraintBottom_toBottomOf=\"parent\"
app:layout_constraintEnd_toEndOf=\"parent\"
app:layout_constraintStart_toStartOf=\"parent\"
app:layout_constraintTop_toBottomOf=\"@+id\/textView2\" \/>
<\/android.support.constraint.ConstraintLayout><\/pre>\n\n\n\n
\"\"

<\/figcaption><\/figure>\n\n\n\n

Uygulaman\u0131n tasar\u0131m\u0131n\u0131 istedi\u011finiz gibi d\u00fczenleyebilirsiniz. \u00d6nemli olan se\u00e7ilen de\u011ferlerin g\u00f6sterilebilece\u011fi bir TextView olmas\u0131d\u0131r. Tabi farkl\u0131 fikirler de olabilir.<\/p>\n\n\n\n

MainActivity.java<\/h2><\/font>\n\n\n\n
public class MainActivity extends AppCompatActivity {

Button button, button2;
TextView textView2,textView3;
Calendar calendar;
DatePickerDialog datePickerDialog;
int hour,minute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main<\/em>);

button = (Button) findViewById(R.id.button<\/em>);
button2 = (Button) findViewById(R.id.button2<\/em>);
textView2 = (TextView) findViewById(R.id.textView2<\/em>);
textView3 = (TextView) findViewById(R.id.textView3<\/em>);

calendar = Calendar.getInstance<\/em>();
hour = calendar.get(Calendar.HOUR_OF_DAY<\/em>);
minute = calendar.get(Calendar.MINUTE<\/em>);

\/\/textView2.setText(hour + \" : \" + minute);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
textView2.setText(hourOfDay + \" : \"+ minute );

}
}, hour,minute,true);
timePickerDialog.show();
}
});


button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar = Calendar.getInstance<\/em>();
int day = calendar.get(Calendar.DAY_OF_MONTH<\/em>);
int month = calendar.get(Calendar.MONTH<\/em>);
int year = calendar.get(Calendar.YEAR<\/em>);

datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
textView3.setText(dayOfMonth + \"\/\" + (month+1) + \"\/\" + year);
}
}, day,month,year);
datePickerDialog.show();
}
});

}

}
<\/pre>\n\n\n\n

Kodlar\u0131 k\u0131saca a\u00e7\u0131klayay\u0131m. Ana s\u0131n\u0131fta \u00f6nemli olan yap\u0131lar TimePickerDialog, DatePickerDialog ve Calendar. Bu s\u0131n\u0131flardan nesne \u00fcretmekle ba\u015fl\u0131yoruz. Butonlara t\u0131kland\u0131\u011f\u0131nda tarih ve saat diyalog pencerelerinin \u00e7\u0131kmas\u0131n\u0131 sa\u011flayan kodlar\u0131, butonlar\u0131n onClick() metodu i\u00e7erisine yaz\u0131yoruz. Integer cinsinden tan\u0131mlad\u0131\u011f\u0131m\u0131z hour<\/em>, minute<\/em>, day<\/em>, month<\/em> ve year<\/em>‘a ise se\u00e7ilen zamanlar atan\u0131r. \u00d6rnek \u27a1 <\/p>\n\n\n\n

hour = calendar.get(Calendar.HOUR_OF_DAY<\/em>);
minute = calendar.get(Calendar.MINUTE<\/em>);
<\/pre>\n\n\n\n

Calendar <\/a>ise takvim s\u0131n\u0131f\u0131d\u0131r. Bu s\u0131n\u0131ftan t\u00fcretti\u011fimiz nesne ile yukar\u0131daki gibi saati ve dakikay\u0131 alabildik. Ve integer cinsindeki hour ve minute de\u011fi\u015fkenlerine atad\u0131k.<\/p>\n\n\n\n

De\u011fi\u015fkenlere atad\u0131\u011f\u0131m\u0131z de\u011ferleri butonlar\u0131n alt\u0131ndaki TextView’lerde g\u00f6sterebilmek i\u00e7in setText<\/em> kullan\u0131yoruz.<\/p>\n\n\n\n

 textView3.setText(dayOfMonth + \"\/\" + (month+1) + \"\/\" + year); 
<\/pre>\n\n\n\n

Butonlar\u0131n onClick()<\/em> metodlar\u0131n\u0131n i\u00e7inde kulland\u0131\u011f\u0131m\u0131z onTimeSet()<\/em> ve onDateSet()<\/em> override metodlar\u0131 sayesinde tarih ve saat kullan\u0131m\u0131n\u0131 daha kolay bir \u015fekilde sa\u011fl\u0131yoruz.<\/p>\n\n\n\n

onTimeSet() ve onDateSet() metodlar\u0131n\u0131n parametrelerinin anlamlar\u0131na da bakal\u0131m.<\/p>\n\n\n\n

 public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) <\/pre>\n\n\n\n
 public void onTimeSet(TimePicker view, int hourOfDay, int minute) <\/pre>\n\n\n\n

hourOfDay <\/strong>: G\u00fcn i\u00e7erisindeki ge\u00e7erli saat.
minuteOfHour :<\/strong> Saat i\u00e7erisindeki ge\u00e7erli dakika.
dayOfMonth :<\/strong> Ay i\u00e7erisindeki ge\u00e7erli g\u00fcn.
hour : <\/strong>saat
minute :<\/strong> dakika
day :<\/strong> g\u00fcn
month :<\/strong> ay
year :<\/strong> y\u0131l<\/p>\n\n\n\n

Son olarak datePickerDialog.show();<\/em> ve timePickerDialog();<\/em> ile se\u00e7ilen tarih ve saati ekranda g\u00f6steriyoruz.<\/p>\n\n\n\n

Uygulaman\u0131n Son Hali : <\/h2><\/font>\n\n\n\n
\"Android
Android – Date Picker & Time Picker Kullan\u0131m\u0131<\/figcaption><\/figure>\n\n\n\n

Bu uygulama sayesinde Android’de Time Picker ve Date Picker kullan\u0131m\u0131n\u0131n<\/a> anla\u015f\u0131ld\u0131\u011f\u0131n\u0131 umuyorum. Siz de uygulaman\u0131za tarih ve saat eklerken bu \u015fekildeki kullan\u0131m\u0131 veya daha farkl\u0131 kullan\u0131mlar\u0131 deneyebilirsiniz. Okudu\u011funuz i\u00e7in te\u015fekk\u00fcr ederim. Bir sonraki yaz\u0131da g\u00f6r\u00fc\u015fmek \u00fczere..????<\/p>\n","protected":false},"excerpt":{"rendered":"

Merhaba arkada\u015flar, bu yaz\u0131da Android uygulamalarda kullan\u0131lan Date Picker ve Time Picker nedir, nas\u0131l kullan\u0131l\u0131r bunlar\u0131 \u00f6\u011frenece\u011fiz ve bununla ilgili Button ve TextView’lerden olu\u015fan bir uygulama yapaca\u011f\u0131z. Gerek web uygulamalarda, gerekse mobil uygulamalarda kullan\u0131lan zaman kavram\u0131 \u00e7ok \u00f6nemlidir. \u015eimdir ise Date Picker ve Time Picker’\u0131n ne oldu\u011funu anlamaya \u00e7al\u0131\u015fal\u0131m. Ve daha sonras\u0131nda da basit bir …<\/p>\n","protected":false},"author":6,"featured_media":1519,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[80,24],"tags":[59,107,109,85,108],"_links":{"self":[{"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/posts\/1515"}],"collection":[{"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/comments?post=1515"}],"version-history":[{"count":18,"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/posts\/1515\/revisions"}],"predecessor-version":[{"id":1725,"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/posts\/1515\/revisions\/1725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/media\/1519"}],"wp:attachment":[{"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/media?parent=1515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/categories?post=1515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codewk.com\/wp-json\/wp\/v2\/tags?post=1515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}