Skip to content

Instantly share code, notes, and snippets.

@aberteau
Last active August 21, 2017 21:42
Show Gist options
  • Save aberteau/64343514b456693568f4003ea7682cb6 to your computer and use it in GitHub Desktop.
Save aberteau/64343514b456693568f4003ea7682cb6 to your computer and use it in GitHub Desktop.
Android Things - DatePickerDialog and TimePickerDialog (stackoverflow.com/questions/45776749)
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.techeasy.androidsamples.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<Button
android:id="@+id/show_datepicker_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DatePickerDialog" />
<Button
android:id="@+id/show_timepicker_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TimePickerDialog" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techeasy.androidsamples">
<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.IOT_LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.things"/>
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.techeasy.androidsamples"
minSdkVersion 24
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
provided 'com.google.android.things:androidthings:0.5-devpreview'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
package com.techeasy.androidsamples;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button showDatePickerButton;
private Button showTimePickerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDatePickerButton = findViewById(R.id.show_datepicker_btn);
showDatePickerButton.setOnClickListener(this);
showTimePickerButton = findViewById(R.id.show_timepicker_btn);
showTimePickerButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view == showDatePickerButton) {
editDate();
}else if(view == showTimePickerButton){
editTime();
}
}
private void editDate() {
DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Log.d(TAG, "onDateSetListener.onDateSet");
}
};
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(this, onDateSetListener, year, month, dayOfMonth);
mDatePicker.show();
}
private void editTime() {
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
Log.d(TAG, "onTimeSetListener.onTimeSet");
}
};
Calendar cal = Calendar.getInstance();
int hours = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(this, onTimeSetListener, hours, minutes, true);
mTimePicker.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment