-
-
Save aberteau/64343514b456693568f4003ea7682cb6 to your computer and use it in GitHub Desktop.
Android Things - DatePickerDialog and TimePickerDialog (stackoverflow.com/questions/45776749)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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