Wednesday, 21 January 2015

DatePickerDialog Example

Date Picker Dialog Example in Android
ScreenShots:





Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Date Picker Dialog</string>
    <string name="button_name">Pick Date</string>

</resources>

Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button_pickdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="@string/button_name" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button_pickdate"
        android:layout_marginTop="35dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

MainActivity.java
package com.swamys.datepickerdialog;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
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;

public class MainActivity extends Activity {

       Calendar c = Calendar.getInstance();
       TextView display;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              Button pickDate = (Button) findViewById(R.id.button_pickdate);
              display = (TextView) findViewById(R.id.textView1);

              pickDate.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           MyDatePicker datePicker = new MyDatePicker();
                           new DatePickerDialog(MainActivity.this, datePicker, c
                                         .get(Calendar.YEAR), c.get(Calendar.MONTH), c
                                         .get(Calendar.DAY_OF_MONTH)).show();
                     }
              });
       }

       class MyDatePicker implements OnDateSetListener {

              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear,
                           int dayOfMonth) {
                     display.setText("Selected Date is :" + dayOfMonth + "-"
                                  + (monthOfYear + 1) + "-" + year);
              }
       }
}



No comments:

Post a Comment