Skip to content

Commit

Permalink
#9 - custom fields and layout files
Browse files Browse the repository at this point in the history
  • Loading branch information
MateStrysewske committed Dec 5, 2016
1 parent a28f9de commit ae7f157
Show file tree
Hide file tree
Showing 9 changed files with 430 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package de.symeda.sormas.app.component;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Date;

import de.symeda.sormas.api.utils.DateHelper;
import de.symeda.sormas.app.R;

/**
* Created by Mate Strysewske on 29.11.2016.
*/

public class DateField extends PropertyField<Date> implements DateFieldInterface {

private TextView dateCaption;
private EditText dateContent;
private Date date;

private InverseBindingListener inverseBindingListener;

public DateField(Context context) {
super(context);
initializeViews(context);
}

public DateField(Context context, AttributeSet attrs) {
super(context, attrs);
initializeViews(context);
}

public DateField(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initializeViews(context);
}

@Override
public void setValue(Date value) {
date = value;
if(value != null) {
dateContent.setText(DateHelper.formatDDMMYYYY(value));
}
}

@Override
public Date getValue() {
return date;
}

@BindingAdapter("android:value")
public static void setValue(DateField view, Date date) {
view.setValue(date);
}

@InverseBindingAdapter(attribute = "android:value", event = "android:valueAttrChanged" /*default - can also be removed*/)
public static Date getValue(DateField view) {
return view.getValue();
}

@BindingAdapter("android:valueAttrChanged")
public static void setListener(DateField view, InverseBindingListener listener) {
view.inverseBindingListener = listener;
}

public void setInputType(int type) {
dateContent.setInputType(type);
}

public void setOnClickListener(OnClickListener listener) {
dateContent.setOnClickListener(listener);
}

public void setOnFocusChangeListener(OnFocusChangeListener listener) {
dateContent.setOnFocusChangeListener(listener);
}

/**
* Inflates the views in the layout.
*
* @param context
* the current context for the view.
*/
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.field_date_field, this);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
dateContent = (EditText) this.findViewById(R.id.date_content);
dateContent.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void afterTextChanged(Editable editable) {
if (inverseBindingListener != null) {
inverseBindingListener.onChange();
}
onValueChanged();
}
});
dateCaption = (TextView) this.findViewById(R.id.date_caption);
dateCaption.setText(getCaption());
}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
dateContent.setEnabled(enabled);
dateCaption.setEnabled(enabled);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.symeda.sormas.app.component;

import android.view.View.*;

/**
* Created by Mate Strysewske on 30.11.2016.
*/

public interface DateFieldInterface {

public void setInputType(int type);
public void setOnClickListener(OnClickListener listener);
public void setOnFocusChangeListener(OnFocusChangeListener listener);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package de.symeda.sormas.app.component;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;

import de.symeda.sormas.api.caze.CaseStatus;
import de.symeda.sormas.app.R;

/**
* Created by Mate Strysewske on 29.11.2016.
*/

public class LabelField extends PropertyField<String> {

private TextView textCaption;
private TextView textContent;

public LabelField(Context context) {
super(context);
initializeViews(context);
}

public LabelField(Context context, AttributeSet attrs) {
super(context, attrs);
initializeViews(context);
}

public LabelField(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initializeViews(context);
}

@Override
public void setValue(String value) {
textContent.setText(value);
}

@Override
public String getValue() {
return textContent.getText().toString();
}

@BindingAdapter("android:value")
public static void setValue(LabelField view, String text) {
view.setValue(text);
}

/**
* Inflates the views in the layout.
*
* @param context
* the current context for the view.
*/
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.field_label_field, this);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
textContent = (TextView) this.findViewById(R.id.text_content);
textCaption = (TextView) this.findViewById(R.id.text_caption);
textCaption.setText(getCaption());
}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
textContent.setEnabled(enabled);
textCaption.setEnabled(enabled);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package de.symeda.sormas.app.component;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.TextView;

import de.symeda.sormas.app.R;

/**
* Created by Mate Strysewske on 28.11.2016.
*/
public class TextField extends PropertyField<String> implements TextFieldInterface {

private EditText textInput;
private TextView textCaption;

private InverseBindingListener inverseBindingListener;

public TextField(Context context) {
super(context);
initializeViews(context);
}

public TextField(Context context, AttributeSet attrs) {
super(context, attrs);
initializeViews(context);
}

public TextField(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initializeViews(context);
}

@Override
public void setValue(String value) {
textInput.setText(value);
}

@Override
public String getValue() {
return textInput.getText().toString();
}

@BindingAdapter("android:value")
public static void setValue(TextField view, String text) {
view.setValue(text);
}

@InverseBindingAdapter(attribute = "android:value", event = "android:valueAttrChanged" /*default - can also be removed*/)
public static String getValue(TextField view) {
return view.getValue();
}

@BindingAdapter("android:valueAttrChanged")
public static void setListener(TextField view, InverseBindingListener listener) {
view.inverseBindingListener = listener;
}

public void updateCaption(String newCaption) {
textCaption.setText(newCaption);
}

/**
* Inflates the views in the layout.
*
* @param context
* the current context for the view.
*/
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.field_text_field, this);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
textInput = (EditText) this.findViewById(R.id.text_input);
textInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void afterTextChanged(Editable editable) {
if (inverseBindingListener != null) {
inverseBindingListener.onChange();
}
onValueChanged();
}
});
textCaption = (TextView) this.findViewById(R.id.text_caption);
textCaption.setText(getCaption());
}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
textInput.setEnabled(enabled);
textCaption.setEnabled(enabled);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.symeda.sormas.app.component;

/**
* Created by Mate Strysewske on 30.11.2016.
*/

public interface TextFieldInterface {

public void updateCaption(String newCaption);

}
21 changes: 21 additions & 0 deletions sormas-app/app/src/main/res/layout/field_date_field.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/date_caption"
android:textSize="@dimen/field_caption_size"
android:layout_marginBottom="@dimen/field_caption_margin_bottom"
android:layout_marginLeft="@dimen/field_caption_margin_left" />
<EditText
android:layout_width="match_parent"
android:layout_height="@dimen/field_height"
android:id="@+id/date_content"/>

</LinearLayout>
</merge>
21 changes: 21 additions & 0 deletions sormas-app/app/src/main/res/layout/field_label_field.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_caption"
android:textSize="@dimen/field_caption_size"
android:layout_marginBottom="@dimen/field_caption_margin_bottom"
android:layout_marginLeft="@dimen/field_caption_margin_left" />
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/field_height"
android:id="@+id/text_content"/>

</LinearLayout>
</merge>
Loading

0 comments on commit ae7f157

Please sign in to comment.