Calendar Adapter for GridView

Quickly share your Android Code Snippets here...

Calendar Adapter for GridView

Postby jrd » Sun Apr 03, 2011 12:57 am

I needed a calendar for a shift pattern app, might be useful to someone:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package im.jsmith.android.calendaractivity;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.GregorianCalendar;
  7. import java.util.List;
  8.  
  9. import android.content.Context;
  10. import android.graphics.Color;
  11. import android.util.DisplayMetrics;
  12. import android.view.Gravity;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.BaseAdapter;
  16. import android.widget.TextView;
  17.  
  18. class MonthAdapter extends BaseAdapter {
  19.         private GregorianCalendar mCalendar;
  20.         private Calendar mCalendarToday;
  21.         private Context mContext;
  22.         private DisplayMetrics mDisplayMetrics;
  23.         private List<String> mItems;
  24.         private int mMonth;
  25.         private int mYear;
  26.         private int mDaysShown;
  27.         private int mDaysLastMonth;
  28.         private int mDaysNextMonth;
  29.         private int mTitleHeight, mDayHeight;
  30.         private final String[] mDays = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
  31.         private final int[] mDaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  32.        
  33.         public MonthAdapter(Context c, int month, int year, DisplayMetrics metrics) {
  34.                 mContext = c;
  35.                 mMonth = month;
  36.                 mYear = year;
  37.                 mCalendar = new GregorianCalendar(mYear, mMonth, 1);
  38.                 mCalendarToday = Calendar.getInstance();
  39.                 mDisplayMetrics = metrics;
  40.                 populateMonth();
  41.         }
  42.        
  43.         private void populateMonth() {
  44.                 mItems = new ArrayList<String>();              
  45.                 for (String day : mDays) {
  46.                         mItems.add(day);
  47.                         mDaysShown++;
  48.                 }
  49.                
  50.                 int firstDay = getDay(mCalendar.get(Calendar.DAY_OF_WEEK));
  51.                 int prevDay;
  52.                 if (mMonth == 0)
  53.                         prevDay = daysInMonth(11) - firstDay + 1;
  54.                 else
  55.                         prevDay = daysInMonth(mMonth - 1) - firstDay + 1;
  56.                 for (int i = 0; i < firstDay; i++) {
  57.                         mItems.add(String.valueOf(prevDay + i));
  58.                         mDaysLastMonth++;
  59.                         mDaysShown++;
  60.                 }
  61.                
  62.                 int daysInMonth = daysInMonth(mMonth); 
  63.                 for (int i = 1; i <= daysInMonth; i++) {
  64.                         mItems.add(String.valueOf(i));
  65.                         mDaysShown++;
  66.                 }
  67.                
  68.                 mDaysNextMonth = 1;
  69.                 while (mDaysShown % 7 != 0) {
  70.                         mItems.add(String.valueOf(mDaysNextMonth));
  71.                         mDaysShown++;
  72.                         mDaysNextMonth++;
  73.                 }
  74.                
  75.                 mTitleHeight = 30;
  76.                 int rows = (mDaysShown / 7);
  77.                 mDayHeight = (mDisplayMetrics.heightPixels - mTitleHeight
  78.                                 - (rows * 3) - getBarHeight())/ (rows - 1);
  79.         }
  80.        
  81.         private int daysInMonth(int month) {
  82.                 int daysInMonth = mDaysInMonth[month];
  83.                 if (month == 1 && mCalendar.isLeapYear(mYear))
  84.                         daysInMonth++;
  85.                 return daysInMonth;
  86.         }
  87.        
  88.         private int getBarHeight() {
  89.                 switch (mDisplayMetrics.densityDpi) {
  90.                 case DisplayMetrics.DENSITY_HIGH:
  91.                         return 48;
  92.                 case DisplayMetrics.DENSITY_MEDIUM:
  93.                         return 32;
  94.                 case DisplayMetrics.DENSITY_LOW:
  95.                         return 24;
  96.                 default:
  97.                         return 48;
  98.                 }
  99.         }
  100.        
  101.         /**
  102.          * For week starting Monday
  103.          * @param day from Calendar.DAY_OF_WEEK
  104.          * @return day week starting Monday
  105.          */
  106.         private int getDay(int day) {
  107.                 switch (day) {
  108.                 case Calendar.MONDAY:
  109.                         return 0;
  110.                 case Calendar.TUESDAY:
  111.                         return 1;
  112.                 case Calendar.WEDNESDAY:
  113.                         return 2;
  114.                 case Calendar.THURSDAY:
  115.                         return 3;
  116.                 case Calendar.FRIDAY:
  117.                         return 4;
  118.                 case Calendar.SATURDAY:
  119.                         return 5;
  120.                 case Calendar.SUNDAY:
  121.                         return 6;
  122.                 default:
  123.                         return 0;
  124.                 }
  125.         }
  126.        
  127.         private boolean isToday(int day, int month, int year) {
  128.                 if (mCalendarToday.get(Calendar.MONTH) == month
  129.                                 && mCalendarToday.get(Calendar.YEAR) == year
  130.                                 && mCalendarToday.get(Calendar.DAY_OF_MONTH) == day) {
  131.                         return true;
  132.                 }
  133.                 return false;
  134.         }
  135.        
  136.         @Override
  137.         public View getView(int position, View convertView, ViewGroup parent) {
  138.                 final TextView view = new TextView(mContext);
  139.                 view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
  140.                 view.setText(mItems.get(position));
  141.                 view.setBackgroundColor(Color.rgb(244, 244, 244));
  142.                 view.setTextColor(Color.BLACK);
  143.  
  144.                 if (position <= 6) {
  145.                         // names
  146.                         view.setBackgroundColor(Color.argb(100, 10, 80, 255));
  147.                         view.setHeight(mTitleHeight);
  148.                 } else if (position <= mDaysLastMonth + 6) {
  149.                         // previous month
  150.                         view.setBackgroundColor(Color.rgb(234, 234, 245));
  151.                         view.setHeight(mDayHeight);
  152.                 } else if (position <= mDaysShown - mDaysNextMonth  ) {
  153.                         // current month
  154.                         view.setHeight(mDayHeight);
  155.                         int day = position - (mDaysLastMonth + 6);
  156.                         if (isToday(day, mMonth, mYear)) {
  157.                                 view.setTextColor(Color.RED);
  158.                         }
  159.                         // check db and colour for shifts
  160.                         // ...
  161.                 } else {
  162.                         // next month
  163.                         view.setHeight(mDayHeight);
  164.                         view.setBackgroundColor(Color.rgb(234, 234, 245));
  165.                 }
  166.                 return view;
  167.         }
  168.        
  169.         @Override
  170.         public int getCount() {
  171.                 return mItems.size();
  172.         }
  173.  
  174.         @Override
  175.         public Object getItem(int position) {
  176.                 return mItems.get(position);
  177.         }
  178.  
  179.         @Override
  180.         public long getItemId(int position) {
  181.                 return position;
  182.         }
  183. }
  184.  
Parsed in 0.051 seconds, using GeSHi 1.0.8.4


Image
jrd
Freshman
Freshman
 
Posts: 3
Joined: Sun Apr 03, 2011 12:52 am

Top

Re: Calendar Adapter for GridView

Postby Fishy » Wed Apr 06, 2011 7:49 pm

Very nice thank you!!!

jrd FTW
Fishy
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Dec 05, 2010 1:13 am

Re: Calendar Adapter for GridView

Postby mapiko » Thu Jun 30, 2011 12:44 pm

Great work. Thank you.
Can you share full code?
mapiko
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat May 15, 2010 5:19 pm

Re: Calendar Adapter for GridView

Postby jrd » Fri Jul 01, 2011 12:52 am

jrd
Freshman
Freshman
 
Posts: 3
Joined: Sun Apr 03, 2011 12:52 am

Re: Calendar Adapter for GridView

Postby milky_way » Sat Sep 17, 2011 4:13 am



url not found ?
milky_way
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Sep 14, 2011 7:20 am

Re: Calendar Adapter for GridView

Postby pavle93 » Mon Dec 12, 2011 2:31 pm

Sorry for necoring, but I would like to know how to use this adapter ?

Thanks in advance :)
pavle93
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 22, 2011 2:32 pm

Top

Re: Calendar Adapter for GridView

Postby pavle93 » Wed Dec 28, 2011 9:45 pm

Bump :DD
pavle93
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 22, 2011 2:32 pm

Re: Calendar Adapter for GridView

Postby lefreak » Tue Feb 21, 2012 11:36 am

Where can I find de XML-fIle for the gui???
lefreak
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 21, 2012 11:32 am

Re: Calendar Adapter for GridView

Postby pavle93 » Tue Feb 21, 2012 8:19 pm

Really how do we use this adapter?
pavle93
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 22, 2011 2:32 pm

Re: Calendar Adapter for GridView

Postby Fulliam » Tue Feb 28, 2012 11:45 am

Did anyone actually manage to download the package? Did the URL ever work? I'm working on something like this and it might make things a lot easier.

I know this thread is very old but I thought it would be worth a try anyway.
current job: autonrengas
Fulliam
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 28, 2012 11:37 am

Re: Calendar Adapter for GridView

Postby aguswgs » Tue Mar 20, 2012 11:10 am

wow this awesome.. I was build it for my phone.. great share :D
aguswgs
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Mar 20, 2012 10:08 am
Location: indonesian

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests