Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

DatePicker


Android DatePicker is a widget to select date. It allows you to select date by day, month and year. Like DatePicker, android also provides TimePicker to select time.

The android.widget.DatePicker is the subclass of FrameLayout class.

Android DatePicker Example

Let’s see the simple example of datepicker widget in android.

activity_main.xml

File: activity_main.xml

  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <TextView
  7.         android:id=“@+id/textView1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginLeft=“50dp”
  13.         android:layout_marginTop=“36dp”
  14.         android:text=“Current Date:” />
  15.     <Button
  16.         android:id=“@+id/button1”
  17.         android:layout_width=“wrap_content”
  18.         android:layout_height=“wrap_content”
  19.         android:layout_alignParentBottom=“true”
  20.         android:layout_centerHorizontal=“true”
  21.         android:layout_marginBottom=“140dp”
  22.         android:text=“Change Date” />
  23.     <DatePicker
  24.         android:id=“@+id/datePicker1”
  25.         android:layout_width=“wrap_content”
  26.         android:layout_height=“wrap_content”
  27.         android:layout_above=“@+id/button1”
  28.         android:layout_centerHorizontal=“true”
  29.         android:layout_marginBottom=“30dp” />
  30. </RelativeLayout>

Activity class

File: MainActivity.java

  1. package com.example.datepicker2;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.DatePicker;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. public class MainActivity extends Activity {
  12.     DatePicker picker;
  13.     Button displayDate;
  14.     TextView textview1;
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.         textview1=(TextView)findViewById(R.id.textView1);
  20.         picker=(DatePicker)findViewById(R.id.datePicker1);
  21.         displayDate=(Button)findViewById(R.id.button1);
  22.         textview1.setText(getCurrentDate());
  23.         displayDate.setOnClickListener(new OnClickListener(){
  24.             @Override
  25.             public void onClick(View view) {
  26.                 textview1.setText(getCurrentDate());
  27.             }
  28.         });
  29.     }
  30.     public String getCurrentDate(){
  31.         StringBuilder builder=new StringBuilder();
  32.         builder.append(“Current Date: “);
  33.         builder.append((picker.getMonth() + 1)+“/”);//month is 0 based
  34.         builder.append(picker.getDayOfMonth()+“/”);
  35.         builder.append(picker.getYear());
  36.         return builder.toString();
  37.     }
  38.     @Override
  39.     public boolean onCreateOptionsMenu(Menu menu) {
  40.         // Inflate the menu; this adds items to the action bar if it is present.
  41.         getMenuInflater().inflate(R.menu.activity_main, menu);
  42.         return true;
  43.     }
  44. }

Comments

comments