Program description:
This program will show how to use date picker dialog.
This program has 3 edit text boxes to enter year, month, and day. Once user enters date user will click on the button to display the date picker.
It will send year, month, and day to date picker dialog from 3 edit text boxes. On showing date picker dialog, user can change the date. Once user changes the date, then latest date from date picker dialog will be displayed in 3 edit text boxes.
First Activity
This program will show how to use date picker dialog.
This program has 3 edit text boxes to enter year, month, and day. Once user enters date user will click on the button to display the date picker.
It will send year, month, and day to date picker dialog from 3 edit text boxes. On showing date picker dialog, user can change the date. Once user changes the date, then latest date from date picker dialog will be displayed in 3 edit text boxes.
First Activity
package com.techpalle.b15_dialogassignment; import android.os.Bundle; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; public class MainActivity extends Activity { //create all the variables.. EditText e1, e2, e3; Button b; public static final int DTPKR_DLG = 1; //Constant for date picker dialog DatePickerDialog.OnDateSetListener lis = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { e1.setText(((Integer)year).toString()); e2.setText(""+monthOfYear); e3.setText(""+dayOfMonth); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initialize all the variables. e1 = (EditText) findViewById(R.id.editText1);//year e2 = (EditText) findViewById(R.id.editText2);//month e3 = (EditText) findViewById(R.id.editText3);//day b = (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(DTPKR_DLG); } }); } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch(id){ case DTPKR_DLG: DatePickerDialog d = (DatePickerDialog) dialog; String year = e1.getText().toString(); String month = e2.getText().toString(); String day = e3.getText().toString(); break; } super.onPrepareDialog(id, dialog); } @Override protected Dialog onCreateDialog(int id) { Dialog d = null; switch(id){ case DTPKR_DLG: String year = e1.getText().toString(); String month = e2.getText().toString(); String day = e3.getText().toString(); DatePickerDialog dp = new DatePickerDialog(this, lis, Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)); d = dp; break; } return d; } }xml layout file for first activity
File name : 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"
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=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:ems="10" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginRight="63dp"
android:text="Set the Date" />
</RelativeLayout>
Download complete code : Click to download
More programs on dialogs:
android Dialogs(alert dialog, datepicker dialog, time picker dialog, progress dialog) tutorial
android custom Dialogs tutorial
Tags : datepicker, datepicker dialog, DatePickerDialog.OnDateSetListener, onDateSetListener
No comments:
Post a Comment