Skip to content

Commit

Permalink
Support Android 5.0 behavior changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hisanobu Tomari committed Oct 20, 2014
1 parent dc5f90b commit 89ccef5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
6 changes: 3 additions & 3 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mooncal"
android:versionCode="5"
android:versionName="5"
android:versionCode="6"
android:versionName="6"
android:installLocation="auto" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="20" />
android:targetSdkVersion="21" />
<supports-screens
android:largeScreens="true"
android:xlargeScreens="true" />
Expand Down
2 changes: 1 addition & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-19
target=android-21
12 changes: 0 additions & 12 deletions res/values-v14/styles.xml

This file was deleted.

4 changes: 4 additions & 0 deletions res/values-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<style name="AppBaseTheme" parent="android:Theme.Material">
</style>
</resources>
51 changes: 30 additions & 21 deletions src/com/example/mooncal/MonthPickerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.widget.DatePicker;

Expand Down Expand Up @@ -37,35 +37,44 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
private DatePickerDialog createMonthPickerDialog() {
DatePickerDialog dpd=new DatePickerDialog(getActivity(),this,year,month,1);
DatePicker datePicker=dpd.getDatePicker();
// Following try block does not work on Android L; does not harm it though
try {
Field[] datePickerFields=datePicker.getClass().getDeclaredFields();
for(Field datePickerField: datePickerFields) {
if("mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
View dayPicker=(View) datePickerField.get(datePicker);
dayPicker.setVisibility(View.GONE);
dpd.setTitle("");

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// Following try block does not work on Android L; does not harm it though
try {
Field[] datePickerFields=datePicker.getClass().getDeclaredFields();
for(Field datePickerField: datePickerFields) {
if("mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
View dayPicker=(View) datePickerField.get(datePicker);
dayPicker.setVisibility(View.GONE);
}
}
} catch (Exception e) {
// ignore
}
} catch (Exception e) {
// ignore
// Workaround Jelly Bean and KitKat DatePickerDialog behavior
// The bug seems to be fixed in the Lollipop, but the change resulted in
// another erroneous behavior when the workaround is there
dpd.setButton(DialogInterface.BUTTON_POSITIVE,
getResources().getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dateActuallySet=true;
}
});
}
dpd.setTitle("");
dpd.setButton(DialogInterface.BUTTON_POSITIVE,
getResources().getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dateActuallySet=true;
}
});

datePicker.setCalendarViewShown(false);
return dpd;
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if(null!=delegate && dateActuallySet) {
if(null!=delegate &&
// JellyBean and KitKat bug workaround
((Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) || dateActuallySet)) {
delegate.onMonthPicked(year, monthOfYear);
}
}
Expand Down

0 comments on commit 89ccef5

Please sign in to comment.