Created
August 2, 2016 15:03
-
-
Save nichtemna/2f142e18ece9f2938b9108fb56cecb9e to your computer and use it in GitHub Desktop.
Weekview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="DayView"> | |
<attr name="day_position" format="integer"/> | |
<attr name="checked" format="boolean"/> | |
</declare-styleable> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DayView extends LinearLayout implements View.OnClickListener { | |
private boolean isChecked = false; | |
private OnCheckedChangeListener changeListener; | |
private TextView textView; | |
private Enums.WeekDays day; | |
public DayView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
initAttrs(attrs); | |
updateView(); | |
} | |
public DayView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
initAttrs(attrs); | |
updateView(); | |
} | |
private void init() { | |
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); | |
View view = inflater.inflate(R.layout.view_day, this, true); | |
textView = (TextView) view.findViewById(R.id.day_tv); | |
setOnClickListener(this); | |
} | |
private void initAttrs(AttributeSet attrs) { | |
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.DayView, 0, 0); | |
try { | |
isChecked = a.getBoolean(R.styleable.DayView_checked, false); | |
int dayPosition = a.getInteger(R.styleable.DayView_day_position, 0); | |
day = Enums.WeekDays.values()[dayPosition]; | |
} finally { | |
a.recycle(); | |
} | |
} | |
public void updateView() { | |
textView.setActivated(isChecked); | |
textView.setText(day.oneLetterResId); | |
} | |
public void setOnCheckedChangeListener(OnCheckedChangeListener changeListener) { | |
this.changeListener = changeListener; | |
} | |
@Override | |
public void onClick(View v) { | |
setChecked(!isChecked); | |
if (changeListener != null) { | |
changeListener.onCheckedChanged(day, isChecked); | |
} | |
} | |
public boolean isChecked() { | |
return isChecked; | |
} | |
public void setChecked(boolean isChecked) { | |
this.isChecked = isChecked; | |
textView.setActivated(isChecked); | |
} | |
public Enums.WeekDays getDay() { | |
return day; | |
} | |
public interface OnCheckedChangeListener { | |
void onCheckedChanged(Enums.WeekDays day, boolean isChecked); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum WeekDays { | |
SUNDAY("Sunday", R.string.sunday_one_letter, R.string.sunday), | |
MONDAY("Monday", R.string.monday_one_letter, R.string.monday), | |
TUESDAY("Tuesday", R.string.tuesday_one_letter, R.string.tuesday), | |
WEDNESDAY("Wednesday", R.string.wednesday_one_letter, R.string.wednesday), | |
THURSDAY("Thursday", R.string.thursday_one_letter, R.string.thursday), | |
FRIDAY("Friday", R.string.friday_one_letter, R.string.friday), | |
SATURDAY("Saturday", R.string.saturday_one_letter, R.string.saturday); | |
public String name; | |
public int oneLetterResId; | |
public int titleResId; | |
WeekDays(String name, int oneLetterResId, int titleResId) { | |
this.name = name; | |
this.oneLetterResId = oneLetterResId; | |
this.titleResId = titleResId; | |
} | |
public static WeekDays getWeekDayFromString(String day) { | |
WeekDays result = null; | |
for (WeekDays weekday : WeekDays.values()) { | |
if (weekday.name.equals(day)) { | |
result = weekday; | |
} | |
} | |
return result; | |
} | |
} | |
<string name="sunday">Sunday</string> | |
<string name="saturday">Saturday</string> | |
<string name="friday">Friday</string> | |
<string name="thursday">Thursday</string> | |
<string name="wednesday">Wednesday</string> | |
<string name="tuesday">Tuesday</string> | |
<string name="monday">Monday</string> | |
<string name="sunday_one_letter">S</string> | |
<string name="saturday_one_letter">S</string> | |
<string name="friday_one_letter">F</string> | |
<string name="thursday_one_letter">T</string> | |
<string name="wednesday_one_letter">W</string> | |
<string name="tuesday_one_letter">T</string> | |
<string name="monday_one_letter">M</string> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<WeekView | |
android:id="@+id/week_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:paddingBottom="10dp" | |
android:paddingLeft="20dp" | |
android:paddingRight="20dp" | |
android:paddingTop="10dp"> | |
<DayView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
app:day_position="0"/> | |
<DayView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
app:day_position="1"/> | |
<DayView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
app:day_position="2"/> | |
<DayView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
app:day_position="3"/> | |
<DayView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
app:day_position="4"/> | |
<DayView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
app:day_position="5"/> | |
<DayView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
app:day_position="6"/> | |
</FrequencyView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WeekView extends LinearLayout implements DayView.OnCheckedChangeListener { | |
private List<DayView> dayViewList = new ArrayList<>(); | |
private List<Enums.WeekDays> days = new ArrayList<>(); | |
private OnFrequencyChangeListener changeListener; | |
public WeekView(Context context) { | |
super(context); | |
} | |
public WeekView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) { | |
if (child instanceof DayView) { | |
DayView button = (DayView) child; | |
button.setOnCheckedChangeListener(this); | |
dayViewList.add(button); | |
} | |
super.addView(child, index, params); | |
} | |
@Override | |
public void onCheckedChanged(Enums.WeekDays day, boolean isChecked) { | |
if (isChecked) { | |
if (!days.contains(day)) { | |
days.add(day); | |
} | |
} else { | |
if (days.contains(day)) { | |
days.remove(day); | |
} | |
} | |
if (changeListener != null) { | |
changeListener.onChange(days); | |
} | |
} | |
public void setDays(List<Enums.WeekDays> days) { | |
this.days = days; | |
for (Enums.WeekDays day : days) { | |
for (DayView dayView : dayViewList) { | |
if (dayView.getDay().equals(day)) { | |
dayView.setChecked(true); | |
} | |
} | |
} | |
} | |
public void setChangeListener(OnFrequencyChangeListener changeListener) { | |
this.changeListener = changeListener; | |
} | |
public interface OnFrequencyChangeListener { | |
void onChange(List<Enums.WeekDays> days); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment