Friday, 22 February 2013

DatePickerDialog in Android

ScreenShots:



activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/chooseDateButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Choose Date"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/displatext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java:
package com.ram.dpdexample;

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;
    int cday, cmonth, cyear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button changeDate = (Button) findViewById(R.id.chooseDateButton);
        display = (TextView) findViewById(R.id.displatext);

        changeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                new DatePickerDialog(MainActivity.this, d,
                        c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
                                .get(Calendar.DAY_OF_MONTH)).show();

            }
        });
    }

    DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {

            cday = dayOfMonth;
            cmonth = monthOfYear + 1;
            cyear = year;

            display.setText("Choosen date is :" + cday + "/" + cmonth + "/"
                    + cyear);
        }
    };
}

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. http://solution2tech.blogspot.in/2013/11/date-picker-in-android.html

    ReplyDelete
  3. hi ! have you made any tutorial regarding reminder application with alarm ?

    ReplyDelete
  4. and can you please help me at this?

    ReplyDelete
  5. import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.app.DatePickerDialog;
    import android.app.DatePickerDialog.OnDateSetListener;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.TextView;
    import android.app.TimePickerDialog;
    import android.app.TimePickerDialog.OnTimeSetListener;
    import android.widget.TimePicker;

    public class MainActivity extends Activity {

    Calendar c=Calendar.getInstance();
    TextView display;
    int cday,cmonth,cyear;
    TextView disptime;
    int chour,cmin,csec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button changeDate=(Button)findViewById(R.id.bselectdate);
    display=(TextView) findViewById(R.id.disptext);
    Button changeTime=(Button)findViewById(R.id.bselecttime);

    changeDate.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    new DatePickerDialog(MainActivity.this,d,
    c.get(Calendar.YEAR),c.get(Calendar.MONTH),
    c.get(Calendar.DAY_OF_MONTH)).show();

    }
    });
    changeTime.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    new DatePickerDialog(MainActivity.this,d,
    c.get(Calendar.HOUR),c.get(Calendar.MINUTE),
    c.get(Calendar.SECOND)).show();

    }
    });
    }
    DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {

    cday = dayOfMonth;
    cmonth = monthOfYear + 1;
    cyear = year;

    display.setText("Choosen date is :" + cday + "/" + cmonth + "/"
    + cyear);
    }
    };

    TimePickerDialog.OnTimeSetListener d1 = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hour, int minute,
    int second) {

    chour = hour;
    cmin = minute + 1;
    csec = second;

    display.setText("Choosen time is :" + chour + "/" + cmin + "/"
    + csec);
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // TODO Auto-generated method stub

    }
    };
    }




    This is my Main Activity code when i am adding time in it it shows calendar...

    ReplyDelete
    Replies
    1. Hi i will check the code..right now i'm very busy...

      Delete
    2. Thank you dear i am waiting for your response

      Delete