Lesson 59. Dialogues. DatePickerDialog
Android Lessons

Lesson 59. Dialogues. DatePickerDialog


In this lesson:

– we use DatePickerDialog

The lesson will be similar to the past, but now in the dialogue we will set the date, not the time. This is called DatePickerDialog.

Let’s make a simple application. TextView will be displayed, you can click on it and a dialog will appear to select the date. When selected, it will be displayed in TextView.

Let’s create a project:

Project name: P0591_DatePickerDialog
Build Target: Android 2.3.3
Application name: DatePickerDialog
Package name: ru.startandroid.develop.p0591datepickerdialog
Create Activity: MainActivity

screen main.xml:



    
    

code MainActivity.java:

package ru.startandroid.develop.p0591datepickerdialog;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity extends Activity {
  
  int DIALOG_DATE = 1;
  int myYear = 2011;
  int myMonth = 02;
  int myDay = 03;
  TextView tvDate;
  
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvDate = (TextView) findViewById(R.id.tvDate);
    }
     
    public void onclick(View view) {
      showDialog(DIALOG_DATE);
    }
    

    protected Dialog onCreateDialog(int id) {
      if (id == DIALOG_DATE) {
        DatePickerDialog tpd = new DatePickerDialog(this, myCallBack, myYear, myMonth, myDay);
        return tpd;
      }
      return super.onCreateDialog(id);
    }
    
    OnDateSetListener myCallBack = new OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
      myYear = year;
      myMonth = monthOfYear;
      myDay = dayOfMonth;
      tvDate.setText("Today is " + myDay + "https://startandroid.ru/" + myMonth + "https://startandroid.ru/" + myYear);
    }
    };
}

IN onCreate we find TextView.

IN onClick call the showDialog method and pass it the dialog ID. This method (showDialog) creates a dialog using a separate method and shows it. The ID is used to specify which dialog to create and display.

method onCreateDialog This is a separate method that is called in showDialog to create a dialog. In this method, we look at what ID came in, create the appropriate dialog and return it.

In our case, we create a DatePickerDialog using the constructor:

DatePickerDialog (Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth), where

context
callBack is a DatePickerDialog.OnDateSetListener handler whose method works when you click OK in the dialog
year is the year that shows the dialogue
monthOfYear – the month that shows the dialogue
dayOfMonth is the day that shows the dialogue

myCallBack – An object that implements the DatePickerDialog.OnDateSetListener interface. He has only one method – onDateSet, which gives us a DatePicker from the dialog, and the year, month and day that he displays. That is what we have introduced in the dialogue.

We write this data on tvDate.

We’ll save everything and run it. Click on TextView. A dialog appears to enter the date.

It now shows March 3, 2011, because when we created it we passed it the values ​​myYear, myMonth and myDay. Months he considers starting from scratch.

change the value

and click Set.

The text showed new meanings. Please note that the month of December is given as 11. The months are numbered from zero.

You can also close the dialog with the Cancel button or the Back button on the emulator. In this case, the handler does not work and the text is not updated.

In the next lesson:

– create AlertDialog
– customize the title, message, picture and buttons




Discuss in the forum [63 replies]

Leave a Reply

Your email address will not be published. Required fields are marked *