Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to add an icon to an event #266

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ Usage
};
```

6. Add an icon at a WeekViewEvent with `WeekViewEvent.setIconId(int iconId)` and change its color with `WeekViewEvent.setIconColor(int iconColor)`.

```java
WeekViewEvent event = new WeekViewEvent(1, "My title", startTime, endTime);

// Add a vector asset
event.setIconId(R.drawable.ic_my_vector);
// Change the color of the asset (by default, it's the same that the title color)
event.setIconColor(R.color.myColor);
```

Customization
-------------------

Expand Down Expand Up @@ -126,6 +137,7 @@ You can customize the look of the `WeekView` in xml. Use the following attribute
- `showNowLine`
- `nowLineColor`
- `nowLineThickness`
- `eventIconSize`

Interfaces
----------
Expand Down
47 changes: 46 additions & 1 deletion library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
Expand Down Expand Up @@ -121,6 +123,7 @@ private enum Direction {
private int mHourSeparatorHeight = 2;
private int mTodayHeaderTextColor = Color.rgb(39, 137, 228);
private int mEventTextSize = 12;
private int mEventIconSize = 12;
private int mEventTextColor = Color.BLACK;
private int mEventPadding = 8;
private int mHeaderColumnBackgroundColor = Color.WHITE;
Expand Down Expand Up @@ -331,6 +334,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
mHourSeparatorHeight = a.getDimensionPixelSize(R.styleable.WeekView_hourSeparatorHeight, mHourSeparatorHeight);
mTodayHeaderTextColor = a.getColor(R.styleable.WeekView_todayHeaderTextColor, mTodayHeaderTextColor);
mEventTextSize = a.getDimensionPixelSize(R.styleable.WeekView_eventTextSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mEventTextSize, context.getResources().getDisplayMetrics()));
mEventIconSize = a.getDimensionPixelSize(R.styleable.WeekView_eventIconSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mEventIconSize, context.getResources().getDisplayMetrics()));
mEventTextColor = a.getColor(R.styleable.WeekView_eventTextColor, mEventTextColor);
mEventPadding = a.getDimensionPixelSize(R.styleable.WeekView_eventPadding, mEventPadding);
mHeaderColumnBackgroundColor = a.getColor(R.styleable.WeekView_headerColumnBackground, mHeaderColumnBackgroundColor);
Expand Down Expand Up @@ -767,6 +771,7 @@ top < getHeight() &&
mEventBackgroundPaint.setColor(mEventRects.get(i).event.getColor() == 0 ? mDefaultEventColor : mEventRects.get(i).event.getColor());
canvas.drawRoundRect(mEventRects.get(i).rectF, mEventCornerRadius, mEventCornerRadius, mEventBackgroundPaint);
drawEventTitle(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas, top, left);
drawEventIcon(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas);
}
else
mEventRects.get(i).rectF = null;
Expand Down Expand Up @@ -804,6 +809,10 @@ private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, floa
int availableHeight = (int) (rect.bottom - originalTop - mEventPadding * 2);
int availableWidth = (int) (rect.right - originalLeft - mEventPadding * 2);

if (event.getIconId() != 0) {
availableWidth -= mEventIconSize + mEventPadding;
}

// Get text dimensions.
StaticLayout textLayout = new StaticLayout(bob, mEventTextPaint, availableWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

Expand Down Expand Up @@ -831,6 +840,42 @@ private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, floa
}


/**
* Draw the icon associated at the event on right bottom of the event rectangle.
*
* @param event The event of which the title (and location) should be drawn.
* @param rect The rectangle on which the text is to be drawn.
* @param canvas The canvas to draw upon.
*/
private void drawEventIcon(WeekViewEvent event, RectF rect, Canvas canvas) {
if (rect.right - rect.left - mEventPadding * 2 - mEventIconSize < 0) return;
if (rect.bottom - rect.top - mEventPadding * 2 - mEventIconSize < 0) return;

int iconId = event.getIconId();

if (iconId != 0) {
Drawable drawable = mContext.getResources().getDrawable(iconId);

if (drawable != null) {
int left = 0;
int top = 0;
int right = mEventIconSize;
int bottom = mEventIconSize;
int color = event.getIconColor() == 0 ? mEventTextColor : event.getIconColor();

drawable.setBounds(left, top, right, bottom);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);

canvas.save();
canvas.translate(rect.right - mEventIconSize - mEventPadding,
rect.bottom - mEventIconSize - mEventPadding);
drawable.draw(canvas);
canvas.restore();
}
}
}


/**
* A class to hold reference to the events and their visual representation. An EventRect is
* actually the rectangle that is drawn on the calendar for a given event. There may be more
Expand Down Expand Up @@ -1972,4 +2017,4 @@ private Calendar today(){
return today;
}

}
}
78 changes: 73 additions & 5 deletions library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ public class WeekViewEvent {
private String mName;
private String mLocation;
private int mColor;
private int mIconId;
private int mIconColor;

public WeekViewEvent(){

}

/**
* Initializes the event for week view.
*
* @param id The id of the event.
* @param name Name of the event.
* @param startYear Year when the event starts.
Expand All @@ -32,8 +35,12 @@ public WeekViewEvent(){
* @param endDay Day when the event ends.
* @param endHour Hour (in 24-hour format) when the event ends.
* @param endMinute Minute when the event ends.
* @param iconId Icon associated at the event.
* @param iconColor Color of the icon associated at the event.
*/
public WeekViewEvent(long id, String name, int startYear, int startMonth, int startDay, int startHour, int startMinute, int endYear, int endMonth, int endDay, int endHour, int endMinute) {
public WeekViewEvent(long id, String name, int startYear, int startMonth, int startDay,
int startHour, int startMinute, int endYear, int endMonth, int endDay,
int endHour, int endMinute, int iconId, int iconColor) {
this.mId = id;

this.mStartTime = Calendar.getInstance();
Expand All @@ -51,35 +58,80 @@ public WeekViewEvent(long id, String name, int startYear, int startMonth, int st
this.mEndTime.set(Calendar.MINUTE, endMinute);

this.mName = name;

this.mIconId = iconId;
this.mIconColor = iconColor;
}

/**
* Initializes the event for week view.
*
* @param id The id of the event.
* @param name Name of the event.
* @param startYear Year when the event starts.
* @param startMonth Month when the event starts.
* @param startDay Day when the event starts.
* @param startHour Hour (in 24-hour format) when the event starts.
* @param startMinute Minute when the event starts.
* @param endYear Year when the event ends.
* @param endMonth Month when the event ends.
* @param endDay Day when the event ends.
* @param endHour Hour (in 24-hour format) when the event ends.
* @param endMinute Minute when the event ends.
*/
public WeekViewEvent(long id, String name, int startYear, int startMonth, int startDay,
int startHour, int startMinute, int endYear, int endMonth, int endDay,
int endHour, int endMinute) {
this(id, name, startYear, startMonth, startDay, startHour, startMinute,
endYear, endMonth, endDay, endHour, endMinute, 0, 0);
}

/**
* Initializes the event for week view.
*
* @param id The id of the event.
* @param name Name of the event.
* @param location The location of the event.
* @param startTime The time when the event starts.
* @param endTime The time when the event ends.
* @param iconId Icon associated at the event.
* @param iconColor Color of the icon associated at the event.
*/
public WeekViewEvent(long id, String name, String location, Calendar startTime, Calendar endTime) {
public WeekViewEvent(long id, String name, String location, Calendar startTime, Calendar endTime,
int iconId, int iconColor) {
this.mId = id;
this.mName = name;
this.mLocation = location;
this.mStartTime = startTime;
this.mEndTime = endTime;
this.mIconId = iconId;
this.mIconColor = iconColor;
}

/**
* Initializes the event for week view.
*
* @param id The id of the event.
* @param name Name of the event.
* @param location The location of the event.
* @param startTime The time when the event starts.
* @param endTime The time when the event ends.
*/
public WeekViewEvent(long id, String name, Calendar startTime, Calendar endTime) {
this(id, name, null, startTime, endTime);
public WeekViewEvent(long id, String name, String location, Calendar startTime, Calendar endTime) {
this(id, name, location, startTime, endTime, 0, 0);
}

/**
* Initializes the event for week view.
*
* @param id The id of the event.
* @param name Name of the event.
* @param startTime The time when the event starts.
* @param endTime The time when the event ends.
*/
public WeekViewEvent(long id, String name, Calendar startTime, Calendar endTime) {
this(id, name, null, startTime, endTime, 0, 0);
}

public Calendar getStartTime() {
return mStartTime;
Expand Down Expand Up @@ -129,6 +181,22 @@ public void setId(long id) {
this.mId = id;
}

public int getIconId() {
return mIconId;
}

public void setIconId(int iconId) {
this.mIconId = iconId;
}

public int getIconColor() {
return mIconColor;
}

public void setIconColor(int iconColor) {
this.mIconColor = iconColor;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -144,4 +212,4 @@ public boolean equals(Object o) {
public int hashCode() {
return (int) (mId ^ (mId >>> 32));
}
}
}
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<attr name="maxHourHeight" format="dimension"/>
<attr name="textSize" format="dimension"/>
<attr name="eventTextSize" format="dimension"/>
<attr name="eventIconSize" format="dimension"/>
<attr name="headerColumnPadding" format="dimension"/>
<attr name="headerRowPadding" format="dimension"/>
<attr name="columnGap" format="dimension"/>
Expand Down