activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ActivityMain" >
 
       <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        
	    <Button 
	    android:layout_width="wrap_content" 
	    android:layout_height="wrap_content"
	    android:text="@string/pick_date" 
	    android:id="@+id/btnDate"
	    android:paddingLeft="15dp"
	    android:paddingRight="15dp"
	    />
              
	    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtDate" 
        android:textSize="20sp"
        android:layout_marginLeft="20dp"
        />
	    
	    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:datePickerMode="spinner"
        android:visibility="gone" />
	    
    </LinearLayout>
    
</RelativeLayout>

ActivityMain.java sınıfı

package tr.com.emrahkahraman.datepicker;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
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 ActivityMain extends ActionBarActivity {

	private TextView txtDate = null;
	private DatePicker datePicker;
	private Button btnDate;

	private int year;
	private int month;
	private int day;
	
	static final int DATE_DIALOG_ID = 999;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		txtDate = (TextView) findViewById(R.id.txtDate);
	    datePicker = (DatePicker) findViewById(R.id.datePicker);
	    btnDate = (Button) findViewById(R.id.btnDate);
	    
		final Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH)+1;
		day = c.get(Calendar.DAY_OF_MONTH);
		txtDate.setText(new StringBuilder().append(padding_str(day))
				.append("/").append(padding_str(month))
				.append("/").append(padding_str(year))
				);

		btnDate.setOnClickListener(clickListener);
	}

	private OnClickListener clickListener = new OnClickListener() 
	   {
		    public void onClick(View v) 
		    {   
		    	switch (v.getId())
		    	{
		    	case R.id.btnDate:
		    		showDialog(DATE_DIALOG_ID);
		    		break;
		    	}
		   }
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, datePickerListener, year,month, day);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener datePickerListener =  new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int selectedYear, int monthOfYear, int dayOfMonth) 
		{
			year = selectedYear;
			month = monthOfYear+1;
			day = dayOfMonth;
			
			txtDate.setText(new StringBuilder().append(padding_str(day))
					.append("/").append(padding_str(month))
					.append("/").append(padding_str(year))
					);
		}
	};
	
	private static String padding_str(int c) 
	{
		if (c >= 10)
		   return String.valueOf(c);
		else
		   return "0" + String.valueOf(c);
	}
}

Ekran Görüntüsü
Screenshot_2015-05-10-16-10-45[1]

Proje Kodunu İndirmek İçin Tıklayınız

Etiketler: ,

Yazar : Emrah Kahraman

Bilgisayar Mühendisi

Android DatePicker Kullanımı Yazısı için Yorum Yapabilirsiniz

Kan Bağışı
Sponsor
Alexa
Hakkımda
Bağlantılar