Read DatePicker and TimePicker

Last exercise show how to implement DatePicker and TimePicker with event Listeners. We can also read it setting without Listener, with get...() methods.

Read DatePicker and TimePicker


package com.example.androiddatepicker;

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity{

DatePicker pickerDate;
TimePicker pickerTime;
Button buttonReadPicker;
TextView info;

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

info = (TextView)findViewById(R.id.info);
pickerDate = (DatePicker)findViewById(R.id.pickerdate);
pickerTime = (TimePicker)findViewById(R.id.pickertime);

Calendar now = Calendar.getInstance();

pickerDate.init(
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);

pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));

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

@Override
public void onClick(View arg0) {
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);

info.setText(cal.toString());

}});
}

}


<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://arteluzevida.blogspot.com/"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:autoLink="web" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<DatePicker
android:id="@+id/pickerdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TimePicker
android:id="@+id/pickertime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/readpicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read Pickers"/>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
</ScrollView>

</LinearLayout>


download filesDownload the files.

Related:
- Set alarm on specified date/time with DatePicker/TimePicker