diff --git a/build.gradle b/build.gradle index 53f4fad..c20bca1 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.1' + classpath 'com.android.tools.build:gradle:2.2.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/sample/build.gradle b/sample/build.gradle index a8a7be0..35f3a30 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 24 - buildToolsVersion "24.0.0" + compileSdkVersion 25 + buildToolsVersion "25.0.0" defaultConfig { applicationId "com.satanasoft.switchdatetimepicker" minSdkVersion 15 - targetSdkVersion 24 + targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -24,7 +24,7 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:24.2.1' + compile 'com.android.support:appcompat-v7:25.0.0' testCompile 'junit:junit:4.12' compile project(path: ':switchdatetime') } diff --git a/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java b/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java index a629d12..9e240b2 100644 --- a/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java +++ b/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java @@ -68,7 +68,6 @@ public void onClick(View view) { public void onSaveInstanceState(Bundle savedInstanceState) { // Save the current textView savedInstanceState.putCharSequence(STATE_TEXTVIEW, textView.getText()); - super.onSaveInstanceState(savedInstanceState); } } diff --git a/switchdatetime/build.gradle b/switchdatetime/build.gradle index 0a1384f..56de398 100644 --- a/switchdatetime/build.gradle +++ b/switchdatetime/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 24 - buildToolsVersion "24.0.0" + compileSdkVersion 25 + buildToolsVersion "25.0.0" defaultConfig { minSdkVersion 15 - targetSdkVersion 24 + targetSdkVersion 25 versionCode 1 versionName "1.0" @@ -26,6 +26,6 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:24.2.1' + compile 'com.android.support:appcompat-v7:25.0.0' testCompile 'junit:junit:4.12' } diff --git a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/AMPMTimePicker.java b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/AMPMTimePicker.java new file mode 100644 index 0000000..3734906 --- /dev/null +++ b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/AMPMTimePicker.java @@ -0,0 +1,131 @@ +package com.kunzisoft.switchdatetime; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.res.Configuration; +import android.os.Build; +import android.util.AttributeSet; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; +import android.widget.TimePicker; + +/** + * A class for solve listener with AM-PM click
+ * Thanks to Velval for init code + * @author J-Jamet + * @version 1.0 + */ +public class AMPMTimePicker extends TimePicker { + + private static final String TAG = "AMPMTimePicker"; + private OnTimeChangedListener onTimeChangedListener; + + public AMPMTimePicker(Context context) { + super(context); + } + + public AMPMTimePicker(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public AMPMTimePicker(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public AMPMTimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + // Stop ScrollView from getting involved once you interact with the View + if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) { + ViewParent p = getParent(); + if (p != null) + p.requestDisallowInterceptTouchEvent(true); + } + return false; + } + + @Override + public void setOnTimeChangedListener(OnTimeChangedListener onTimeChangedListener) { + super.setOnTimeChangedListener(onTimeChangedListener); + this.onTimeChangedListener = onTimeChangedListener; + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + init(); + } + + @SuppressWarnings("deprecation") + private void init() { + try { + ViewGroup amPmView; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + + // LinearLayout (LOLLIPOP) + // GridLayout (M-LANDSCAPE) + // LinearLayout (M-PORTRAIT) + ViewGroup v1 = (ViewGroup) getChildAt(0); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + + // FrameLayout (LOLLIPOP-LANDSCAPE) + // FrameLayout - id:time_header (LOLLIPOP-PORTRAIT) + ViewGroup v2 = (ViewGroup) v1.getChildAt(0); + + // FrameLayout - id:TimeHeader (LOLLIPOP-LANDSCAPE) + // LinearLayout (LOLLIPOP-PORTRAIT) + ViewGroup v3 = (ViewGroup) v2.getChildAt(0); + + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { + ViewGroup v4 = (ViewGroup) v3.getChildAt(0); // LinearLayout (LOLLIPOP) + amPmView = (ViewGroup) v4.getChildAt(3); // LinearLayout - id:ampm_layout (LOLLIPOP) + } else { // PORTRAIT + amPmView = (ViewGroup) v3.getChildAt(3); // LinearLayout - id:ampm_layout (LOLLIPOP) + } + } else { // M and after + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { + ViewGroup v2 = (ViewGroup) v1.getChildAt(1); // RelativeLayout (M) + amPmView = (ViewGroup) v2.getChildAt(1); // LinearLayout - id:ampm_layout (M) + } else { + ViewGroup v2 = (ViewGroup) v1.getChildAt(0); // RelativeLayout - id:time_header (M) + amPmView = (ViewGroup) v2.getChildAt(3); // LinearLayout - id:ampm_layout (M) + } + } + + View am = amPmView.getChildAt(0); // AppCompatCheckedTextView - id:am_label + View pm = amPmView.getChildAt(1); // AppCompatCheckedTextView - id:pm_label + + View.OnTouchListener listener = new View.OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + int hour; + int minute; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + hour = getCurrentHour(); + minute = getCurrentMinute(); + } else { + hour = getHour(); + minute = getMinute(); + } + hour = (hour >= 12) ? hour - 12 : hour + 12; + onTimeChangedListener.onTimeChanged(AMPMTimePicker.this, hour, minute); + return false; + } + }; + am.setOnTouchListener(listener); + pm.setOnTouchListener(listener); + } + } catch (Exception e) { + Log.e(TAG, "TimePicker is not defined for this Android version : " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java index f2fc9d3..08aaa8f 100644 --- a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java +++ b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java @@ -162,7 +162,7 @@ else if (switcher.getDisplayedChild() == 1) { }); // Construct TimePicker - TimePicker timePicker = (TimePicker) dialog.findViewById(R.id.timePicker); + TimePicker timePicker = (AMPMTimePicker) dialog.findViewById(R.id.timePicker); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { timePicker.setHour(hour); } else { diff --git a/switchdatetime/src/main/res/layout-land/dialog_switch_datetime_picker.xml b/switchdatetime/src/main/res/layout-land/dialog_switch_datetime_picker.xml index dbab7cb..6bb4727 100644 --- a/switchdatetime/src/main/res/layout-land/dialog_switch_datetime_picker.xml +++ b/switchdatetime/src/main/res/layout-land/dialog_switch_datetime_picker.xml @@ -12,11 +12,11 @@ android:inAnimation="@anim/dialog_enter_from_top" android:outAnimation="@anim/dialog_leave_to_bottom" android:layout_gravity="center"> - - + - - +