Wednesday, July 11, 2012

DatePicker in Android



Click Here To Download Source Code

Package Name   :   selva.datepicker

Project Name     :  DatePicker

Version              :  1.5 (Supports  1.5 and above versions)


main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Date" />

</LinearLayout>


DatePickerActivity.java


 package selva.datepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

public class DatePickerActivity extends Activity {
  
    DatePicker datePicker;
  
      
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        datePicker = (DatePicker) findViewById(R.id.datePicker1);
      
        Button btnOpen = (Button) findViewById(R.id.button1);
      
        btnOpen.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Toast.makeText(getBaseContext(),"Date selected:" + datePicker.getMonth() +
                        "/" + datePicker.getDayOfMonth() +
                        "/" + datePicker.getYear()  ,Toast.LENGTH_SHORT).show();
        }
        });
      
      
      
    }
}


 OUTPUT:




























click GetDate Button










































Click Here To Download Source Code


No comments:

Post a Comment