Description

1. Date Picker is a control which provides user flexibility to pick the date.

2. It has a month, day and year picking user interface format calendar.

3. It is a part of Widget

4. Button, datePicker and TextView controls are used.

5. onClickListener and datePicker() method is introduced to invoke the button class

XML Attributes

android:calendarViewShow: it checks the status of calendar i view. 

android:datePickerMode: it shows the look of widget. 

android:maxDate : The maximum date shown by this calendar view. 

android:minDate: The minimum date shown by this calendar view. 

android:spinnersShown : It checks whether spinner is shown. 

android:startYear : it set first year of calender. 

android:yearListItemTextAppearance :To Change the list year's text appearance in the list. 

android:yearListSelectorColor : To change the list year's selected circle color in the list.

Methods

getDayOfMonth() :- It returns the selected day of month.

getMonth() :It get the selected month.

getYear() : It returns the selected year.

getCalendarView():-To get the calendar view..

Android_Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.date">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mapsample.msclient009.date.MainActivity"
    android:orientation="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <DatePicker
        android:id="@+id/dp"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:textColor="#199119"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#F90"
        android:textSize="20dp"
        android:textColor="#f9f9f9"
        android:text="Date-Picker Tool" />
    </LinearLayout>
</LinearLayout>


Main_Activity.java
package com.mapsample.msclient009.date;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
 DatePicker dp;
 Button btn;
 TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        dp =(DatePicker)findViewById ( R.id.dp );
        btn =(Button)findViewById ( R.id.btn );
        tv =(TextView)findViewById ( R.id.tv );
          tv.setText (datePicker () );
        btn.setOnClickListener ( new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                tv.setText ( datePicker () );
            }
        } );
    }
    public String datePicker()
    {StringBuilder sb = new StringBuilder ( );
      sb.append ("Select Date");
      sb.append ( dp.getMonth ()+1+":" );
      sb.append (dp.getDayOfMonth ()+":");
      sb.append ( dp.getYear () );
      return (sb.toString ());
    }
}

DatePicker in Android  DatePicker in Android

  Modified On Sep-07-2019 12:17:57 AM

Leave Comment