Skip to content

Commit acca541

Browse files
committed
Fix #1 bug with AM-PM click
1 parent 3de0401 commit acca541

File tree

8 files changed

+145
-15
lines changed

8 files changed

+145
-15
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.1'
8+
classpath 'com.android.tools.build:gradle:2.2.2'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

sample/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "24.0.0"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.0"
66
defaultConfig {
77
applicationId "com.satanasoft.switchdatetimepicker"
88
minSdkVersion 15
9-
targetSdkVersion 24
9+
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -24,7 +24,7 @@ dependencies {
2424
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2525
exclude group: 'com.android.support', module: 'support-annotations'
2626
})
27-
compile 'com.android.support:appcompat-v7:24.2.1'
27+
compile 'com.android.support:appcompat-v7:25.0.0'
2828
testCompile 'junit:junit:4.12'
2929
compile project(path: ':switchdatetime')
3030
}

sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public void onClick(View view) {
6868
public void onSaveInstanceState(Bundle savedInstanceState) {
6969
// Save the current textView
7070
savedInstanceState.putCharSequence(STATE_TEXTVIEW, textView.getText());
71-
7271
super.onSaveInstanceState(savedInstanceState);
7372
}
7473
}

switchdatetime/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "24.0.0"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.0"
66

77
defaultConfig {
88
minSdkVersion 15
9-
targetSdkVersion 24
9+
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
1212

@@ -26,6 +26,6 @@ dependencies {
2626
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2727
exclude group: 'com.android.support', module: 'support-annotations'
2828
})
29-
compile 'com.android.support:appcompat-v7:24.2.1'
29+
compile 'com.android.support:appcompat-v7:25.0.0'
3030
testCompile 'junit:junit:4.12'
3131
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.kunzisoft.switchdatetime;
2+
3+
import android.annotation.TargetApi;
4+
import android.content.Context;
5+
import android.content.res.Configuration;
6+
import android.os.Build;
7+
import android.util.AttributeSet;
8+
import android.util.Log;
9+
import android.view.MotionEvent;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.view.ViewParent;
13+
import android.widget.TimePicker;
14+
15+
/**
16+
* A class for solve listener with AM-PM click <br />
17+
* Thanks to Velval for init code
18+
* @author J-Jamet
19+
* @version 1.0
20+
*/
21+
public class AMPMTimePicker extends TimePicker {
22+
23+
private static final String TAG = "AMPMTimePicker";
24+
private OnTimeChangedListener onTimeChangedListener;
25+
26+
public AMPMTimePicker(Context context) {
27+
super(context);
28+
}
29+
30+
public AMPMTimePicker(Context context, AttributeSet attrs) {
31+
super(context, attrs);
32+
}
33+
34+
public AMPMTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
35+
super(context, attrs, defStyleAttr);
36+
}
37+
38+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
39+
public AMPMTimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
40+
super(context, attrs, defStyleAttr, defStyleRes);
41+
}
42+
43+
@Override
44+
public boolean onInterceptTouchEvent(MotionEvent ev) {
45+
// Stop ScrollView from getting involved once you interact with the View
46+
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
47+
ViewParent p = getParent();
48+
if (p != null)
49+
p.requestDisallowInterceptTouchEvent(true);
50+
}
51+
return false;
52+
}
53+
54+
@Override
55+
public void setOnTimeChangedListener(OnTimeChangedListener onTimeChangedListener) {
56+
super.setOnTimeChangedListener(onTimeChangedListener);
57+
this.onTimeChangedListener = onTimeChangedListener;
58+
}
59+
60+
@Override
61+
protected void onFinishInflate() {
62+
super.onFinishInflate();
63+
init();
64+
}
65+
66+
@SuppressWarnings("deprecation")
67+
private void init() {
68+
try {
69+
ViewGroup amPmView;
70+
71+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
72+
73+
// LinearLayout (LOLLIPOP)
74+
// GridLayout (M-LANDSCAPE)
75+
// LinearLayout (M-PORTRAIT)
76+
ViewGroup v1 = (ViewGroup) getChildAt(0);
77+
78+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
79+
80+
// FrameLayout (LOLLIPOP-LANDSCAPE)
81+
// FrameLayout - id:time_header (LOLLIPOP-PORTRAIT)
82+
ViewGroup v2 = (ViewGroup) v1.getChildAt(0);
83+
84+
// FrameLayout - id:TimeHeader (LOLLIPOP-LANDSCAPE)
85+
// LinearLayout (LOLLIPOP-PORTRAIT)
86+
ViewGroup v3 = (ViewGroup) v2.getChildAt(0);
87+
88+
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
89+
ViewGroup v4 = (ViewGroup) v3.getChildAt(0); // LinearLayout (LOLLIPOP)
90+
amPmView = (ViewGroup) v4.getChildAt(3); // LinearLayout - id:ampm_layout (LOLLIPOP)
91+
} else { // PORTRAIT
92+
amPmView = (ViewGroup) v3.getChildAt(3); // LinearLayout - id:ampm_layout (LOLLIPOP)
93+
}
94+
} else { // M and after
95+
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
96+
ViewGroup v2 = (ViewGroup) v1.getChildAt(1); // RelativeLayout (M)
97+
amPmView = (ViewGroup) v2.getChildAt(1); // LinearLayout - id:ampm_layout (M)
98+
} else {
99+
ViewGroup v2 = (ViewGroup) v1.getChildAt(0); // RelativeLayout - id:time_header (M)
100+
amPmView = (ViewGroup) v2.getChildAt(3); // LinearLayout - id:ampm_layout (M)
101+
}
102+
}
103+
104+
View am = amPmView.getChildAt(0); // AppCompatCheckedTextView - id:am_label
105+
View pm = amPmView.getChildAt(1); // AppCompatCheckedTextView - id:pm_label
106+
107+
View.OnTouchListener listener = new View.OnTouchListener() {
108+
@Override
109+
public boolean onTouch(View v, MotionEvent event) {
110+
int hour;
111+
int minute;
112+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
113+
hour = getCurrentHour();
114+
minute = getCurrentMinute();
115+
} else {
116+
hour = getHour();
117+
minute = getMinute();
118+
}
119+
hour = (hour >= 12) ? hour - 12 : hour + 12;
120+
onTimeChangedListener.onTimeChanged(AMPMTimePicker.this, hour, minute);
121+
return false;
122+
}
123+
};
124+
am.setOnTouchListener(listener);
125+
pm.setOnTouchListener(listener);
126+
}
127+
} catch (Exception e) {
128+
Log.e(TAG, "TimePicker is not defined for this Android version : " + e.getMessage());
129+
}
130+
}
131+
}

switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ else if (switcher.getDisplayedChild() == 1) {
162162
});
163163

164164
// Construct TimePicker
165-
TimePicker timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
165+
TimePicker timePicker = (AMPMTimePicker) dialog.findViewById(R.id.timePicker);
166166
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
167167
timePicker.setHour(hour);
168168
} else {

switchdatetime/src/main/res/layout-land/dialog_switch_datetime_picker.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
android:inAnimation="@anim/dialog_enter_from_top"
1313
android:outAnimation="@anim/dialog_leave_to_bottom"
1414
android:layout_gravity="center">
15-
<TimePicker
15+
<com.kunzisoft.switchdatetime.AMPMTimePicker
1616
android:id="@+id/timePicker"
1717
android:layout_width="@dimen/pickerWidth"
1818
android:layout_height="match_parent">
19-
</TimePicker>
19+
</com.kunzisoft.switchdatetime.AMPMTimePicker>
2020
<DatePicker
2121
android:id="@+id/datePicker"
2222
android:layout_width="@dimen/pickerWidth"

switchdatetime/src/main/res/layout/dialog_switch_datetime_picker.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@
5454
android:inAnimation="@anim/dialog_enter_from_right"
5555
android:outAnimation="@anim/dialog_leave_to_left"
5656
android:layout_gravity="center">
57-
<TimePicker
57+
<com.kunzisoft.switchdatetime.AMPMTimePicker
5858
android:id="@+id/timePicker"
5959
android:layout_gravity="center"
6060
android:layout_width="wrap_content"
6161
android:layout_height="match_parent">
62-
</TimePicker>
62+
</com.kunzisoft.switchdatetime.AMPMTimePicker>
6363
<DatePicker
6464
android:id="@+id/datePicker"
6565
android:layout_gravity="center"

0 commit comments

Comments
 (0)