postheadericon Android DatePickerDialog Example



MainActivity.java

package com.bipin.rupadiya.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 {

TextView tv;

Calendar myCal = Calendar.getInstance();

DatePickerDialog.OnDateSetListener dpl = new OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCal.set(Calendar.YEAR, year);
myCal.set(Calendar.MONTH, monthOfYear);
myCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
tv.setText("" + dayOfMonth + " / " + (monthOfYear + 1) + " / " + year );

}
};

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

tv = (TextView) findViewById(R.id.textView1);

Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

DatePickerDialog dpd = new DatePickerDialog(arg0.getContext(),
dpl, myCal.get(Calendar.YEAR), myCal
.get(Calendar.MONTH), myCal
.get(Calendar.DAY_OF_MONTH));
dpd.show();
}
});

}

}



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/btnDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="135dp"
        android:text="Date" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnDate"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="100dp"
        android:text="Select Date"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="www.BipinRupadiya.com" />

</RelativeLayout>





0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.