Activities in Android – Tutorial | AndroidMonks

Activity in Android – Tutorial | AndroidStudio The activity class is a very important part of a Android Application. The paradigm shift in the world of smartphone applications begins here!!How exciting is it to know, from your whatsapp to facebook application, nothing can ever happen without the Activities. Think of activities as the starting block, … Read more Activities in Android – Tutorial | AndroidMonks

Android Notifications – Tutorial | AndroidMonks

Android Notifications – Complete Tutorial Notifications in android provides a way to interact with user outside the application. In Android the notification area is present when you swipe down from the Top. When you want to send out a notification to this area from your application (Either from a Activity or a Service), you will … Read more Android Notifications – Tutorial | AndroidMonks

Bounded Service Android With Example | AndroidMonks

Bounded Service in Android – Complete Tutorial If you are looking for a Service that can stick for the lifetime of a Activity and destroyed once the Activity is destroyed, bounded service is your answer. Service in Android mainly consists of Background, Foreground and Bounded Service. In this tutorial we will see about Bounded Service … Read more Bounded Service Android With Example | AndroidMonks

Foreground Service in Android [Update 2020]

Foreground Notification

Foreground Service in Android – Android Tutorial This article has been updated on 2/1/2020 to make sure that the Foreground Services are working fine even with the Android Pie and above also. Service in Android is a very important concept. Foreground Service is part of the Service, that decides how your application can be made … Read more Foreground Service in Android [Update 2020]

Service in Android – Android Tutorial

Service in Android – Tutorial in Android Studio As important as an Activity is for android, Service is really important to provide the ability to handle lot of activities like, performing background tasks (Like playing songs, interacting with a  server) and foreground tasks as well. It does not have any UI and acts without any … Read more Service in Android – Android Tutorial

Kotlin on Android – Overview and Introduction Tutorial

Kotlin on Android – What is it? Kotlin has kept ringing for sometime now and people are already hyping a lot about it. Ever since Android hit the world of smartphone it has proved to be very very versatile. However, it is at its core Java and seeing how Java 8 and issues which were … Read more Kotlin on Android – Overview and Introduction Tutorial

Android Fragments Tutorial – Android Programming

Android Fragments Tutorial – Android Programming Fragments in android is a pretty confusing topic to start off. It is often used along with the activity and lot of tutorials talk about how a fragment is bound to the lifecycle of the activity. We will see what a fragment is and how to create one to … Read more Android Fragments Tutorial – Android Programming

Android 9 Pie – Machine Intelligence, The future?

Android 9 Pie Release – A peek into the future? After spending more than a week analysing what the latest release from Android 9 Pie has got, i am pretty much convinced that, we are stepping into the future of mobile technology. Mobile smartphone technology has grown to a point and has halted for the … Read more Android 9 Pie – Machine Intelligence, The future?

DatePicker in Android – Implementation and Overview

DatePicker in Android – Implementation and Overview

Continuation to the TimePicker, DatePicker is used to provide a UI to get the dates from the user. It is pretty important and a stable View which is used frequently. There are two types of Date Picker present in Android, calendar view and spinner view like below.

Spinner View DatePicker
Spinner View DatePicker
Calendar view Datepicker
Calendar view Datepicker

DatePicker – XML implementation

We will use the <DatePicker> tag inside the layout file like below.

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

The default mode is the calendar view and we will see about the properties of the Date Picker to change mode between spinner and calendar.

DatePicker – Important Attributes

There are few but important attributes to DatePicker.

You can check a list of all other attributes here

DatePicker – Activity Implementation

Let us create a simple Activity to implement our Date Picker.

Before going to that, there are several public methods that are used inside the class to manipulate the calendar view. They are given below

Important Public Methods

  • getDayOfMonth(): Retrieves the day of the month as set by the user in the Date Picker
  • getMonth(): Retrieves the month as set by the user from the Date Picker
  • getYear(): Retrieves the year as set by the user from the Date Picker
  • setSpinnersShown(boolean): You can modify the Date Picker to be either spinner or calendar view from the activity
Sample Application:

For our sample application we will use, Date Picker, Textview and a Button.

date_picker.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<DatePicker
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:calendarTextColor="#444"
    android:startYear="2000"
    android:endYear="2019"
    android:id="@+id/date_picker"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_date"
        android:text="submit"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view_time"
        android:textColor="@color/colorAccent"
        android:textSize="55sp"
        android:layout_gravity="center"/>
</LinearLayout>

DatePickerActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Date;

public class DatePickerActivity extends AppCompatActivity {
    TextView textView;
    DatePicker datePicker;
    Button dateClicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date_picker);
        textView = (TextView) findViewById(R.id.text_view_time);
        datePicker = (DatePicker) findViewById(R.id.date_picker);
        dateClicker = (Button) findViewById(R.id.button_date);
        dateClicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(datePicker.getDayOfMonth()+":"+datePicker.getMonth()+":"+datePicker.getYear());
            }
        });
    }
}

Resulting Application:

DatePicker Android
DatePicker Android

 

DatePicker – Material Design Ideas

You can check here for some very cool material design ideas for you next project.

Dont stop experimenting, you can create your custom themed Date Picker as well.

Drop in any comments you have below.

“Learn and be curious”

Read moreDatePicker in Android – Implementation and Overview

TimePicker in Android – Implementation and Overview

TimePicker in Android – Implementation and Overview TimePicker provides a UI to select time. There are two modes that are available, one is through using the standard clock like interface and the other is the spinner type to choose in hours:minutes:seconds format.  in our layout file. <TimePicker android:layout_width=”match_parent” android:layout_height=”match_parent”/> Default attributes are layout_height and layout_width. … Read more TimePicker in Android – Implementation and Overview