From a10f1a99cd5f0ecea2f55152ee54d7e94bd2b187 Mon Sep 17 00:00:00 2001 From: Dhananjay Patel <> Date: Thu, 17 May 2018 20:40:44 +0530 Subject: [PATCH 1/2] Week view added --- AndroidCalendar/.idea/misc.xml | 15 +- .../CalendarWeekViewPagerAdapter.java | 76 ++++++++++ .../androidcalendar/objects/CalendarDate.java | 1 + .../androidcalendar/objects/CalendarWeek.java | 88 ++++++++++++ .../views/CalendarWeekView.java | 93 +++++++++++++ .../views/CustomWeekCalendarView.java | 131 ++++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 2 +- 7 files changed, 391 insertions(+), 15 deletions(-) create mode 100644 AndroidCalendar/app/src/main/java/com/androidcalendar/adapters/CalendarWeekViewPagerAdapter.java create mode 100644 AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java create mode 100644 AndroidCalendar/app/src/main/java/com/androidcalendar/views/CalendarWeekView.java create mode 100644 AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomWeekCalendarView.java diff --git a/AndroidCalendar/.idea/misc.xml b/AndroidCalendar/.idea/misc.xml index 5d19981..ba7052b 100644 --- a/AndroidCalendar/.idea/misc.xml +++ b/AndroidCalendar/.idea/misc.xml @@ -1,8 +1,5 @@ - - - - - - - - - - - - - - + diff --git a/AndroidCalendar/app/src/main/java/com/androidcalendar/adapters/CalendarWeekViewPagerAdapter.java b/AndroidCalendar/app/src/main/java/com/androidcalendar/adapters/CalendarWeekViewPagerAdapter.java new file mode 100644 index 0000000..f7cff3e --- /dev/null +++ b/AndroidCalendar/app/src/main/java/com/androidcalendar/adapters/CalendarWeekViewPagerAdapter.java @@ -0,0 +1,76 @@ +package com.androidcalendar.adapters; + +import android.support.v4.view.PagerAdapter; +import android.view.View; +import android.view.ViewGroup; + +import com.androidcalendar.objects.CalendarWeek; +import com.androidcalendar.views.CalendarWeekView; + +import java.util.List; + + +public class CalendarWeekViewPagerAdapter extends PagerAdapter { + + List mData; + + public CalendarWeekViewPagerAdapter(List list) { + mData = list; + } + + @Override + public Object instantiateItem(ViewGroup container, int position) { + CalendarWeek month = mData.get(position); + CalendarWeekView calendarView = new CalendarWeekView(container.getContext()); + calendarView.buildView(month); + (container).addView(calendarView, 0); + calendarView.setTag(month); + return calendarView; + } + + @Override + public int getCount() { + return mData.size(); + } + + @Override + public void destroyItem(ViewGroup collection, int position, Object view) { + collection.removeView((View) view); + } + + @Override + public boolean isViewFromObject(View view, Object object) { + return view == object; + } + + @Override + public int getItemPosition(Object object) { + View view = (View) object; + CalendarWeek month = (CalendarWeek) view.getTag(); + int position = mData.indexOf(month); + + if (position >= 0) { + return position; + } else { + return POSITION_NONE; + } + } + + public void addNext(CalendarWeek month) { + mData.add(month); + notifyDataSetChanged(); + } + + public void addPrev(CalendarWeek month) { + mData.add(0, month); + notifyDataSetChanged(); + } + + public String getItemPageHeader(int position) { + return mData.get(position).toString(); + } + + public CalendarWeek getItem(int position) { + return mData.get(position); + } +} diff --git a/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarDate.java b/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarDate.java index b9a8b48..fb86c7e 100644 --- a/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarDate.java +++ b/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarDate.java @@ -32,6 +32,7 @@ public CalendarDate(long millis) { mYear = calendar.get(Calendar.YEAR); } + public int getDay() { return mDay; } diff --git a/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java b/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java new file mode 100644 index 0000000..16e487e --- /dev/null +++ b/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java @@ -0,0 +1,88 @@ +package com.androidcalendar.objects; + +import android.util.Log; + +import com.androidcalendar.utils.Utils; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + + +public class CalendarWeek { + + public static final int NUMBER_OF_WEEKS_IN_WEEK = 1; + public static final int NUMBER_OF_DAYS_IN_WEEK = 7; + private int mMonth; + private int mYear; + private int mWeekOfMonth; + private int mWeekOfYear; + private List mDays; + private int mCurrentDayOfMonth; + + public CalendarWeek(Calendar calendar) { + mMonth = calendar.get(Calendar.MONTH); + mYear = calendar.get(Calendar.YEAR); + mWeekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH); + mWeekOfYear = calendar.get(Calendar.WEEK_OF_YEAR); + mCurrentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); + createWeekDays(); + } + + public CalendarWeek(CalendarWeek other, int value) { + Calendar calendar = Calendar.getInstance(); + + // set calendarWeek + calendar.set(other.mYear, other.mMonth, other.mCurrentDayOfMonth); + + // add number of weeks to current calendar + calendar.add(Calendar.WEEK_OF_YEAR, value); + + + mMonth = calendar.get(Calendar.MONTH); + mYear = calendar.get(Calendar.YEAR); + mWeekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH); + mWeekOfYear = calendar.get(Calendar.WEEK_OF_YEAR); + mCurrentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); + createWeekDays(); + } + + private void createWeekDays() { + Calendar calendar = Calendar.getInstance(); + calendar.set(mYear, mMonth, mCurrentDayOfMonth); + int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + calendar.add(Calendar.DATE, 1-dayOfWeek); + + Log.d("TEST", "Day of week :"+dayOfWeek+ " DAte :"+calendar.getTime().toString()); + + CalendarDate date = new CalendarDate(calendar.get(Calendar.DAY_OF_MONTH), mMonth, mYear); + + mDays = new ArrayList<>(); + for (int i = 0; i < NUMBER_OF_DAYS_IN_WEEK; i++) { + CalendarDate day = new CalendarDate(date.getDay(), date.getMonth(), date.getYear()); + mDays.add(day); + date.addDays(1); + } + } + + public int getMonth() { + return mMonth; + } + + public int getYear() { + return mYear; + } + + public int getCurrentDayOfMonth() { + return mCurrentDayOfMonth; + } + + public List getDays() { + return mDays; + } + + @Override + public String toString() { + return mCurrentDayOfMonth + " " + Utils.monthToString(mMonth) + " " + mYear; + } +} diff --git a/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CalendarWeekView.java b/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CalendarWeekView.java new file mode 100644 index 0000000..08f7eba --- /dev/null +++ b/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CalendarWeekView.java @@ -0,0 +1,93 @@ +package com.androidcalendar.views; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.GridLayout; +import android.widget.TextView; + +import com.androidcalendar.R; +import com.androidcalendar.objects.CalendarDate; +import com.androidcalendar.objects.CalendarWeek; +import com.androidcalendar.utils.Utils; + + +public class CalendarWeekView extends FrameLayout { + + private GridLayout mGridLayout; + private ViewGroup mLayoutDays; + + public CalendarWeekView(Context context) { + super(context); + init(); + } + + public CalendarWeekView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public CalendarWeekView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + private void init() { + inflate(getContext(), R.layout.view_calendar_month, this); + mGridLayout = (GridLayout) findViewById(R.id.view_calendar_month_grid); + mLayoutDays = (ViewGroup) findViewById(R.id.view_calendar_month_layout_days); + } + + public void buildView(CalendarWeek calendarWeek) { + buildDaysLayout(); + buildGridView(calendarWeek); + } + + private void buildDaysLayout() { + String[] days; + days = getResources().getStringArray(R.array.days_sunday_array); + + for (int i = 0; i < mLayoutDays.getChildCount(); i++) { + TextView textView = (TextView) mLayoutDays.getChildAt(i); + textView.setText(days[i]); + } + } + + private void buildGridView(CalendarWeek calendarWeek) { + int row = CalendarWeek.NUMBER_OF_WEEKS_IN_WEEK; + int col = CalendarWeek.NUMBER_OF_DAYS_IN_WEEK; + mGridLayout.setRowCount(row); + mGridLayout.setColumnCount(col); + + int screenWidth = Utils.getScreenWidth(getContext()); + int width = screenWidth / col; + + for (CalendarDate day : calendarWeek.getDays()) { + CalendarDayView dayView = new CalendarDayView(getContext(), day); + GridLayout.LayoutParams params = new GridLayout.LayoutParams(); + params.width = width; + params.height = LayoutParams.WRAP_CONTENT; + + dayView.setLayoutParams(params); + dayView.setTextDay("" + day.getDay()); + decorateDayView(dayView, day, calendarWeek.getMonth()); + mGridLayout.addView(dayView); + } + } + + protected void decorateDayView(CalendarDayView dayView, CalendarDate day, int month) { + +// if (day.getMonth() != month) { +// dayView.setOtherMothTextColor(); +// } else { +// dayView.setThisMothTextColor(); +// } + + dayView.setThisMothTextColor(); + + if (day.isToday()) { + dayView.setPurpleSolidOvalBackground(); + } + } +} diff --git a/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomWeekCalendarView.java b/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomWeekCalendarView.java new file mode 100644 index 0000000..08537df --- /dev/null +++ b/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomWeekCalendarView.java @@ -0,0 +1,131 @@ +package com.androidcalendar.views; + +import android.content.Context; +import android.support.annotation.AttrRes; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.ImageButton; +import android.widget.TextView; + +import com.androidcalendar.R; +import com.androidcalendar.adapters.CalendarWeekViewPagerAdapter; +import com.androidcalendar.objects.CalendarWeek; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +/** + * Created by Dhananjay Patel on 4/19/17. + */ + +public class CustomWeekCalendarView extends FrameLayout implements View.OnClickListener { + + private TextView mPagerTextMonth; + private ImageButton mButtonLeftArrow; + private ImageButton mButtonRightArrow; + private ViewPager mViewPager; + private CalendarWeekViewPagerAdapter mViewPagerAdapter; + + public CustomWeekCalendarView(@NonNull Context context) { + super(context); + init(); + } + + public CustomWeekCalendarView(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + init(); + } + + public CustomWeekCalendarView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + private void init() { + inflate(getContext(), R.layout.view_custom_calendar, this); + mViewPager = (ViewPager) findViewById(R.id.activity_main_view_pager); + mPagerTextMonth = (TextView) findViewById(R.id.activity_main_pager_text_month); + mButtonLeftArrow = (ImageButton) findViewById(R.id.activity_main_pager_button_left_arrow); + mButtonRightArrow = (ImageButton) findViewById(R.id.activity_main_pager_button_right_arrow); + mButtonLeftArrow.setOnClickListener(this); + mButtonRightArrow.setOnClickListener(this); + buildCalendarView(); + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.activity_main_pager_button_right_arrow: + int next = mViewPager.getCurrentItem() + 1; + mViewPager.setCurrentItem(next, true); + break; + case R.id.activity_main_pager_button_left_arrow: + int prev = mViewPager.getCurrentItem() - 1; + mViewPager.setCurrentItem(prev, true); + break; + } + } + + + private void buildCalendarView() { + List list = new ArrayList<>(); + CalendarWeek today = new CalendarWeek(Calendar.getInstance()); + + list.add(new CalendarWeek(today, -2)); + list.add(new CalendarWeek(today, -1)); + list.add(today); + list.add(new CalendarWeek(today, 1)); + list.add(new CalendarWeek(today, 2)); + + mViewPagerAdapter = new CalendarWeekViewPagerAdapter(list); + mViewPager.setAdapter(mViewPagerAdapter); + mViewPager.addOnPageChangeListener(mPageChangeListener); + mViewPager.setOffscreenPageLimit(1); + mViewPager.setCurrentItem(2); + mPagerTextMonth.setText(mViewPagerAdapter.getItemPageHeader(2)); + } + + private ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + + } + + @Override + public void onPageScrollStateChanged(int state) { + int position = mViewPager.getCurrentItem(); + mPagerTextMonth.setText(mViewPagerAdapter.getItemPageHeader(position)); + + // current item is the first item in the list + if (state == ViewPager.SCROLL_STATE_IDLE && position == 1) { + addPrev(); + } + + // current item is the last item in the list + if (state == ViewPager.SCROLL_STATE_IDLE && position == mViewPagerAdapter.getCount() - 2) { + addNext(); + } + + } + }; + + private void addNext() { + CalendarWeek week = mViewPagerAdapter.getItem(mViewPagerAdapter.getCount() - 1); + mViewPagerAdapter.addNext(new CalendarWeek(week, 1)); + } + + private void addPrev() { + CalendarWeek week = mViewPagerAdapter.getItem(0); + mViewPagerAdapter.addPrev(new CalendarWeek(week, -1)); + } +} diff --git a/AndroidCalendar/app/src/main/res/layout/activity_main.xml b/AndroidCalendar/app/src/main/res/layout/activity_main.xml index 001fd9e..cd1b42a 100644 --- a/AndroidCalendar/app/src/main/res/layout/activity_main.xml +++ b/AndroidCalendar/app/src/main/res/layout/activity_main.xml @@ -63,7 +63,7 @@ android:textSize="30sp"/> - \ No newline at end of file From c9bafc7000332327a19b786105f3db07642f9a63 Mon Sep 17 00:00:00 2001 From: Dhananjay Patel <> Date: Fri, 18 May 2018 15:22:42 +0530 Subject: [PATCH 2/2] removed day in month title --- .../androidcalendar/objects/CalendarWeek.java | 6 +++--- .../views/CustomCalendarView.java | 2 +- .../app/src/main/res/layout/activity_main.xml | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java b/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java index 16e487e..c5382a4 100644 --- a/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java +++ b/AndroidCalendar/app/src/main/java/com/androidcalendar/objects/CalendarWeek.java @@ -51,9 +51,9 @@ private void createWeekDays() { Calendar calendar = Calendar.getInstance(); calendar.set(mYear, mMonth, mCurrentDayOfMonth); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); - calendar.add(Calendar.DATE, 1-dayOfWeek); + calendar.add(Calendar.DATE, 1 - dayOfWeek); - Log.d("TEST", "Day of week :"+dayOfWeek+ " DAte :"+calendar.getTime().toString()); + Log.d("TEST", "Day of week :" + dayOfWeek + " DAte :" + calendar.getTime().toString()); CalendarDate date = new CalendarDate(calendar.get(Calendar.DAY_OF_MONTH), mMonth, mYear); @@ -83,6 +83,6 @@ public List getDays() { @Override public String toString() { - return mCurrentDayOfMonth + " " + Utils.monthToString(mMonth) + " " + mYear; + return Utils.monthToString(mMonth) + " " + mYear; } } diff --git a/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomCalendarView.java b/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomCalendarView.java index dff1bd6..1594dea 100644 --- a/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomCalendarView.java +++ b/AndroidCalendar/app/src/main/java/com/androidcalendar/views/CustomCalendarView.java @@ -103,9 +103,9 @@ public void onPageSelected(int position) { @Override public void onPageScrollStateChanged(int state) { + int position = mViewPager.getCurrentItem(); mPagerTextMonth.setText(mViewPagerAdapter.getItemPageHeader(position)); - // current item is the first item in the list if (state == ViewPager.SCROLL_STATE_IDLE && position == 1) { addPrev(); diff --git a/AndroidCalendar/app/src/main/res/layout/activity_main.xml b/AndroidCalendar/app/src/main/res/layout/activity_main.xml index cd1b42a..d7d8632 100644 --- a/AndroidCalendar/app/src/main/res/layout/activity_main.xml +++ b/AndroidCalendar/app/src/main/res/layout/activity_main.xml @@ -1,8 +1,8 @@ + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + android:textSize="80sp" /> + android:background="@color/white" /> + android:src="@drawable/ic_date_range_white_24dp" /> + android:background="@color/white" /> + android:textSize="30sp" /> + android:layout_height="wrap_content" /> \ No newline at end of file