Skip to content

Commit

Permalink
Modern date/time form field
Browse files Browse the repository at this point in the history
  • Loading branch information
eltos committed Feb 13, 2019
1 parent e2a386f commit bde75d9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ public DateTimeViewHolder buildViewHolder() {
///////////////////////////////////////////////////////////////////////////////////////////


// protected @ColorInt int getInitialColor(Context context){
// if (presetId != NO_ID){
// return context.getResources().getColor(presetId);
// }
// return preset;
// }


protected DateTime(Parcel in) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand All @@ -47,9 +47,8 @@ class DateTimeViewHolder extends FormElementViewHolder<DateTime> implements Simp
SAVED_MINUTE = "minute";
private static final String DATE_DIALOG_TAG = "datePickerDialogTag",
TIME_DIALOG_TAG = "timePickerDialogTag";
private TextView label;
private EditText date;
private EditText time;
private EditText date, time;
private TextInputLayout dateLayout, timeLayout;
private Long day;
private Integer hour, minute;

Expand All @@ -66,35 +65,21 @@ protected int getContentViewLayout() {
protected void setUpView(View view, final Context context, Bundle savedInstanceState,
final SimpleFormDialog.DialogActions actions) {

label = (TextView) view.findViewById(R.id.label);
date = (EditText) view.findViewById(R.id.date);
time = (EditText) view.findViewById(R.id.time);
dateLayout = (TextInputLayout) view.findViewById(R.id.dateLayout);
timeLayout = (TextInputLayout) view.findViewById(R.id.timeLayout);

// Label
String text = field.getText(context);
label.setText(text);
label.setVisibility(text == null ? View.GONE : View.VISIBLE);
// label.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// colorView.performClick();
// }
// });

// Color preset
// if (savedInstanceState != null) {
// colorView.setColor(savedInstanceState.getInt(SAVED_COLOR));
// } else {
// colorView.setColor(field.getInitialColor(context));
// }
// colorView.setOutlineWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
// 2 /*dp*/, context.getResources().getDisplayMetrics()));
// colorView.setOutlineColor(field.outline);
// colorView.setStyle(ColorView.Style.PALETTE);
// colorView.setChecked(true);
//
//
date.setVisibility(field.type == DateTime.Type.DATETIME
if (text != null){
dateLayout.setHint(text);
if (field.type == DateTime.Type.TIME){
timeLayout.setHint(text);
}
}

dateLayout.setVisibility(field.type == DateTime.Type.DATETIME
|| field.type == DateTime.Type.DATE ? View.VISIBLE : View.GONE);

date.setOnClickListener(new View.OnClickListener() {
Expand All @@ -112,7 +97,7 @@ public void onClick(View v) {
});


time.setVisibility(field.type == DateTime.Type.DATETIME
timeLayout.setVisibility(field.type == DateTime.Type.DATETIME
|| field.type == DateTime.Type.TIME ? View.VISIBLE : View.GONE);

time.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -147,6 +132,7 @@ private void updateText(){
date.setText(day == null ? null : SimpleDateFormat.getDateInstance().format(new Date(day)));
time.setText(hour == null || minute == null ? null :
SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(new Date(0, 0, 0, hour, minute)));

}


Expand Down Expand Up @@ -191,14 +177,14 @@ protected boolean posButtonEnabled(Context context) {
@Override
protected boolean validate(Context context) {
boolean valid = posButtonEnabled(context);
date.setError(null);
time.setError(null);
dateLayout.setErrorEnabled(false);
timeLayout.setErrorEnabled(false);
if (!valid) {
if (day == null){
date.setError(context.getString(R.string.required));
dateLayout.setError(context.getString(R.string.required));
}
if (hour == null || minute == null){
time.setError(context.getString(R.string.required));
timeLayout.setError(context.getString(R.string.required));
}
}
return valid;
Expand All @@ -209,6 +195,7 @@ public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle ex
if ((DATE_DIALOG_TAG+field.resultKey).equals(dialogTag)){
if (which == BUTTON_POSITIVE){
day = extras.getLong(SimpleDateDialog.DATE);
dateLayout.setErrorEnabled(false);
if (field.type == DateTime.Type.DATETIME){
time.performClick();
}
Expand All @@ -222,6 +209,7 @@ public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle ex
if (which == BUTTON_POSITIVE){
hour = extras.getInt(SimpleTimeDialog.HOUR);
minute = extras.getInt(SimpleTimeDialog.MINUTE);
timeLayout.setErrorEnabled(false);
} else if (which == BUTTON_NEGATIVE){
hour = null;
minute = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,47 @@
<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="wrap_content">
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
<android.support.design.widget.TextInputLayout
android:id="@+id/dateLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="4dp"
android:saveEnabled="false"
android:focusable="false"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Date:" />
app:counterOverflowTextAppearance="@style/OverflowText">

<EditText
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/date"
android:focusable="false"
android:inputType="date" />
<android.support.design.widget.TextInputEditText
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:saveEnabled="false"
android:hint="@string/date"
android:focusable="false"
android:inputType="date"/>

</android.support.design.widget.TextInputLayout>

<EditText
android:id="@+id/time"
android:layout_width="wrap_content"
<android.support.design.widget.TextInputLayout
android:id="@+id/timeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/time"
android:saveEnabled="false"
android:focusable="false"
android:inputType="time" />
android:layout_weight="1"
app:counterOverflowTextAppearance="@style/OverflowText">

<android.support.design.widget.TextInputEditText
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:saveEnabled="false"
android:hint="@string/time"
android:focusable="false"
android:inputType="date"/>

</android.support.design.widget.TextInputLayout>

</LinearLayout>

0 comments on commit bde75d9

Please sign in to comment.