DatePicker in Android


Using DatePicker user can set a specific date by setting the day,month and year of DatePIcker.I am going to show you an example,There will be two buttons btnset and btndisplay.When user will click on btnset it will display  a dialog box and user will select the date and after selecting date when user will click on btndisplay it will display the date previously selected by user.

1.) Create a Project DatePickerExample.
2.) Create a date_picker.xml in res/layout.
3.) Put the following code in date_picker.xml :

 
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

  >

<Button
   android:id="@+id/btnset"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Set" />

<Button
   android:id="@+id/btndisplay"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Display" />

</LinearLayout>

DatePickerExample class:

import android.app.Activity;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
     public class DatePickerExample extends Activity {
     
        private int mYear, mMonth, mDay;
        private int yr,month,day;
        static final int DATE_DIALOG_ID = 1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.date_picker);

                final Calendar c = Calendar.getInstance();
                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);
                updateDisplay();
     
             
                Button date = (Button) findViewById(R.id.btnset);
                date.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                showDialog(DATE_DIALOG_ID);
                        }
                });

               
                Button display = (Button) findViewById(R.id.btndisplay);
                display.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                        Toast.makeText(ctx,"Date:" +day+"/"+month+"/"+ yr,Toast.LENGTH_SHORT).show();
                        }
                });
               
                }
              @Override
              protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:
                        return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);
                }
                return null;
             }
     
          private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                int dayOfMonth) {
                        yr = year;
                        month = monthOfYear;
                        day = dayOfMonth;
                     
                }
        };
        }

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular Posts