Skip to content

Commit

Permalink
Merge pull request #4 from granoeste/master
Browse files Browse the repository at this point in the history
Added support OnClick & Custom java.text.DateFormat
  • Loading branch information
Jonatan E. Salas authored May 1, 2017
2 parents c4e57d0 + 1726d66 commit caae39c
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 51 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "io.blackbox_vision.dateedittextsample"
minSdkVersion 14
Expand All @@ -25,8 +25,8 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'

compile project(':datetimepickeredittext')

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/io/blackbox_vision/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ protected void onCreate(Bundle savedInstanceState) {

datePickerInputEditText.setManager(getSupportFragmentManager());
timePickerInputEditText.setManager(getSupportFragmentManager());

// Set the date formatter with the given formatting style from device setting.
datePickerEditText.setDateFormat(android.text.format.DateFormat.getLongDateFormat(getApplicationContext()));
timePickerEditText.setTimeFormat(android.text.format.DateFormat.getTimeFormat(getApplicationContext()));

datePickerInputEditText.setDateFormat(android.text.format.DateFormat.getMediumDateFormat(getApplicationContext()));
timePickerInputEditText.setTimeFormat(android.text.format.DateFormat.getTimeFormat(getApplicationContext()));
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/select_time"
app:timeFormat="HH:mm"
app:timeFormat="kk:mm"
app:is24HourView="true"/>

</android.support.design.widget.TextInputLayout>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 3 additions & 3 deletions datetimepickeredittext/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 14
Expand All @@ -27,8 +27,8 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'

testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.format.DateFormat;
import android.util.Log;

import java.text.ParseException;
Expand All @@ -15,19 +16,19 @@ public final class DateUtils {
private static final String LOG_TAG = DateUtils.class.getSimpleName();

private static final String DATE_TEMPLATE = "dd/MM/yyyy";
private static final String TIME_TEMPLATE = "HH:mm";
private static final String TIME_TEMPLATE = "kk:mm";

private DateUtils() { }

private static String format(@NonNull final Date date, @NonNull final String template) {
return new SimpleDateFormat(template, Locale.getDefault()).format(date);
private static CharSequence format(@NonNull final Date date, @NonNull final String template) {
return DateFormat.format(template, date);
}

public static String toDate(@NonNull final Date date, @Nullable final String template) {
public static CharSequence toDate(@NonNull final Date date, @Nullable final String template) {
return format(date, null != template ? template : DATE_TEMPLATE);
}

public static String toTime(@NonNull final Date date, @Nullable final String template) {
public static CharSequence toTime(@NonNull final Date date, @Nullable final String template) {
return format(date, null != template ? template : TIME_TEMPLATE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.AppCompatEditText;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.EditText;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;

Expand All @@ -19,10 +20,11 @@
import io.blackbox_vision.datetimepickeredittext.internal.utils.DateUtils;

import static android.view.View.OnFocusChangeListener;
import static android.view.View.OnClickListener;
import static android.app.DatePickerDialog.OnDateSetListener;


public final class DatePickerEditText extends EditText implements OnFocusChangeListener, OnDateSetListener {
public final class DatePickerEditText extends AppCompatEditText implements OnFocusChangeListener, OnClickListener, OnDateSetListener {
private static final String TAG = DatePickerEditText.class.getSimpleName();

private OnFocusChangeListener onFocusChangedListener;
Expand All @@ -35,6 +37,8 @@ public final class DatePickerEditText extends EditText implements OnFocusChangeL
private String minDate;
private String maxDate;

private DateFormat textDateFormat;

private Calendar date;

public DatePickerEditText(Context context) {
Expand All @@ -56,6 +60,7 @@ public DatePickerEditText(Context context, AttributeSet attrs, int defStyleAttr)

private void init() {
setOnFocusChangeListener(this);
setOnClickListener(this);
setInputType(InputType.TYPE_NULL);
}

Expand All @@ -81,21 +86,30 @@ public void onFocusChange(View view, boolean isFocused) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);

if (isFocused) {
final DatePickerFragment datePickerFragment = new DatePickerFragment()
.setDate(date)
.setThemeId(themeId)
.setOnDateSetListener(this)
.setMinDate(minDate)
.setMaxDate(maxDate);

datePickerFragment.show(manager, TAG);
showDatePicker();
}

if (null != onFocusChangedListener) {
onFocusChangedListener.onFocusChange(view, isFocused);
}
}

@Override
public void onClick(View v) {
showDatePicker();
}

private void showDatePicker() {
final DatePickerFragment datePickerFragment = new DatePickerFragment()
.setDate(date)
.setThemeId(themeId)
.setOnDateSetListener(this)
.setMinDate(minDate)
.setMaxDate(maxDate);

datePickerFragment.show(manager, TAG);
}

@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
final Calendar calendar = Calendar.getInstance(Locale.getDefault());
Expand All @@ -104,7 +118,11 @@ public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayO
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

setText(DateUtils.toDate(calendar.getTime(), dateFormat));
if (textDateFormat != null) {
setText(textDateFormat.format(calendar.getTime()));
} else {
setText(DateUtils.toDate(calendar.getTime(), dateFormat));
}
date = calendar;
}

Expand Down Expand Up @@ -144,6 +162,11 @@ public DatePickerEditText setDateFormat(String dateFormat) {
return this;
}

public DatePickerEditText setDateFormat(DateFormat format) {
this.textDateFormat = format;
return this;
}

public Integer getThemeId() {
return themeId;
}
Expand All @@ -170,4 +193,5 @@ public DatePickerEditText setMinDate(String minDate) {
this.minDate = minDate;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;

Expand All @@ -19,10 +20,11 @@
import io.blackbox_vision.datetimepickeredittext.internal.utils.DateUtils;

import static android.view.View.OnFocusChangeListener;
import static android.view.View.OnClickListener;
import static android.app.DatePickerDialog.OnDateSetListener;


public final class DatePickerInputEditText extends TextInputEditText implements OnFocusChangeListener, OnDateSetListener {
public final class DatePickerInputEditText extends TextInputEditText implements OnFocusChangeListener, OnClickListener, OnDateSetListener {
private static final String TAG = DatePickerInputEditText.class.getSimpleName();

private OnFocusChangeListener onFocusChangedListener;
Expand All @@ -35,6 +37,8 @@ public final class DatePickerInputEditText extends TextInputEditText implements
private String minDate;
private String maxDate;

private DateFormat textDateFormat;

private Calendar date;

public DatePickerInputEditText(Context context) {
Expand All @@ -56,6 +60,7 @@ public DatePickerInputEditText(Context context, AttributeSet attrs, int defStyle

private void init() {
setOnFocusChangeListener(this);
setOnClickListener(this);
setInputType(InputType.TYPE_NULL);
}

Expand All @@ -81,21 +86,30 @@ public void onFocusChange(View view, boolean isFocused) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);

if (isFocused) {
final DatePickerFragment datePickerFragment = new DatePickerFragment()
.setDate(date)
.setThemeId(themeId)
.setOnDateSetListener(this)
.setMinDate(minDate)
.setMaxDate(maxDate);

datePickerFragment.show(manager, TAG);
showDatePicker();
}

if (null != onFocusChangedListener) {
onFocusChangedListener.onFocusChange(view, isFocused);
}
}

@Override
public void onClick(View v) {
showDatePicker();
}

private void showDatePicker() {
final DatePickerFragment datePickerFragment = new DatePickerFragment()
.setDate(date)
.setThemeId(themeId)
.setOnDateSetListener(this)
.setMinDate(minDate)
.setMaxDate(maxDate);

datePickerFragment.show(manager, TAG);
}

@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
final Calendar calendar = Calendar.getInstance(Locale.getDefault());
Expand All @@ -104,7 +118,11 @@ public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayO
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

setText(DateUtils.toDate(calendar.getTime(), dateFormat));
if (textDateFormat != null) {
setText(textDateFormat.format(calendar.getTime()));
} else {
setText(DateUtils.toDate(calendar.getTime(), dateFormat));
}
date = calendar;
}

Expand Down Expand Up @@ -144,6 +162,11 @@ public DatePickerInputEditText setDateFormat(String dateFormat) {
return this;
}

public DatePickerInputEditText setDateFormat(DateFormat format) {
this.textDateFormat = format;
return this;
}

public Integer getThemeId() {
return themeId;
}
Expand Down
Loading

0 comments on commit caae39c

Please sign in to comment.