Create CalenderDemo calss
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
import java.util.Locale;
public class CalenderDemo extends AppCompatActivity implements View.OnClickListener, CalenderViewAdapter.Test {
String TAG = getClass().getName();
private TextView currentMonth;
LinearLayout prevMonth, nextMonth;
private GridView calendarView;
private CalenderViewAdapter adapter;
private Calendar _calendar;
private int month, year;
private final DateFormat dateFormatter = new DateFormat();
private static final String dateTemplate = "MMMM yyyy";
RelativeLayout dynamicMenuViewRelativeView;
CalenderDemo act = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calender_demo);
initializeView();
_calendar = Calendar.getInstance(Locale.getDefault());
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);
currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()));
adapter = new CalenderViewAdapter(getApplicationContext(), R.id.calendar_day_grid_cell, month, year, this);
calendarView.setAdapter(adapter);
adapter.notifyDataSetChanged();
//getJobsOfUser();
/* calendarView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(getApplicationContext(), "calendarView", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "calendarView" + parent + " " + position + " " + id, Toast.LENGTH_LONG).show();
}
});*/
/* calendarView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), "calendarView", Toast.LENGTH_LONG).show();
}
});*/
}
private void initializeView() {
prevMonth = (LinearLayout) this.findViewById(R.id.prev_month);
currentMonth = (TextView) this.findViewById(R.id.current_month);
nextMonth = (LinearLayout) this.findViewById(R.id.next_month);
calendarView = (GridView) this.findViewById(R.id.calender_grid);
dynamicMenuViewRelativeView = (RelativeLayout) findViewById(R.id.add_menu_view_relative_layout);
prevMonth.setOnClickListener(this);
nextMonth.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.prev_month:
if (month <= 1) {
month = 12;
year--;
} else {
month--;
}
setGridCellAdapterToDate(month, year);
break;
case R.id.next_month:
if (month > 11) {
month = 1;
year++;
} else {
month++;
}
setGridCellAdapterToDate(month, year);
break;
}
}
private void setGridCellAdapterToDate(int month, int year) {
adapter = new CalenderViewAdapter(getApplicationContext(), R.id.calendar_day_grid_cell, month, year, this);
_calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()));
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
}
@Override
public void onDateClick(String date) {
Toast.makeText(this, "oks" + date, Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
}
}
Xml activity_calender_demo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#FFF">
<GridView
android:clickable="true"
android:id="@+id/calender_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rl"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="#fff"
android:numColumns="7">
</GridView>
<View
android:id="@+id/view_of_line"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/calender_grid"
android:layout_marginTop="15dp"
android:background="#7aac7e">
</View>
<!-- <ListView
android:id="@+id/job_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linear_layout_below_list_view"
android:layout_below="@+id/view_of_line"
android:layout_marginBottom="@dimen/small_size"
android:layout_marginLeft="@dimen/medium_size"
android:layout_marginTop="@dimen/small_size"
android:divider="@android:color/transparent"
android:dividerHeight="0dp">
</ListView>-->
<!--<TextView
android:id="@+id/list_empty_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linear_layout_below_list_view"
android:layout_below="@+id/view_of_line"
android:layout_marginBottom="@dimen/small_size"
android:layout_marginLeft="@dimen/medium_size"
android:layout_marginTop="@dimen/small_size"
android:text="@string/no_jobs_available"/>
-->
<!-- <LinearLayout
android:id="@+id/linear_layout_below_list_view"
android:layout_width="fill_parent"
android:layout_height="@dimen/edtText_height"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/small_size"
android:layout_marginLeft="@dimen/small_size"
android:layout_marginRight="@dimen/small_size"
android:background="@drawable/radio_button_selector"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:id="@+id/completed_linear_layout"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/completed_layout_background_checked"
android:clickable="true"
android:focusable="true"
android:gravity="center">
<TextView
android:id="@+id/show_completed_job_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Completed" />
</LinearLayout>
<LinearLayout
android:id="@+id/active_linear_layout"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:background="@drawable/active_layout_background_unchecked"
android:focusable="true"
android:gravity="center"
>
<TextView
android:id="@+id/show_active_job_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Active" />
</LinearLayout>
<!–nag–>
<LinearLayout
android:id="@+id/runbal_linear_layout"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:background="@drawable/active_layout_background_unchecked"
android:focusable="true"
android:gravity="center"
>
<TextView
android:id="@+id/run_active_job_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Posted" />
</LinearLayout>
</LinearLayout>-->
<RelativeLayout
android:id="@+id/add_menu_view_relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout>
<LinearLayout
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fcfafa"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/add_menu_view_relative_layout"
android:layout_toEndOf="@+id/add_menu_view_relative_layout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp">
<LinearLayout
android:id="@+id/prev_month"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#79209c">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#79209c" />
</LinearLayout>
<TextView
android:id="@+id/current_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="JANUARY 2015"
android:textSize="16dp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/next_month"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:background="#79209c">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:background="#79209c" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:weightSum="7">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Su"
/>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Mo" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Tu" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="We" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Th" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Fr" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Sa" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Create CalenderViewAdapter
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
public class CalenderViewAdapter extends BaseAdapter implements OnClickListener {
private static final String tag = "GridCellAdapter";
private final Context _context;
ArrayList model;
Test test;
String getdat= "29/3/2017";
private final List<String> list;
private static final int DAY_OFFSET = 1;
private final String[] weekdays = new String[]{"Su", "Mo", "Tu", "We",
"Th", "Fr", "Sa"};
private final String[] months = {"January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"};
private final int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31};
private int daysInMonth;
private int currentDayOfMonth;
private int currentWeekDay;
private TextView gridcell;
RelativeLayout mRelative_Root;
// private final HashMap<String, Integer> eventsPerMonthMap;
Activity av;
private final SimpleDateFormat dateFormatter = new SimpleDateFormat(
"dd-MMM-yyyy");
boolean dot_check_flag = false;
String dddddd ;
public CalenderViewAdapter(Context context, int calendarDayGridcell, int month, int year, Test test) {
super();
this._context = context;
this.list = new ArrayList<String>();
Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
+ "Year: " + year);
Calendar calendar = Calendar.getInstance();
setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());
this.test = test;
printMonth(month, year);
// Find Number of Events
// eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
}
public interface Test {
public void onDateClick(String date);
}
private String getMonthAsString(int i) {
return months[i];
}
private String getWeekDayAsString(int i) {
return weekdays[i];
}
private int getNumberOfDaysOfMonth(int i) {
return daysOfMonth[i];
}
private void printMonth(int mm, int yy) {
Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
int trailingSpaces = 0;
int daysInPrevMonth = 0;
int prevMonth = 0;
int prevYear = 0;
int nextMonth = 0;
int nextYear = 0;
int currentMonth = mm - 1;
String currentMonthName = getMonthAsString(currentMonth);
daysInMonth = getNumberOfDaysOfMonth(currentMonth);
Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
+ daysInMonth + " days.");
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());
if (currentMonth == 11) {
prevMonth = currentMonth - 1;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = yy;
nextYear = yy + 1;
Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
} else if (currentMonth == 0) {
prevMonth = 11;
prevYear = yy - 1;
nextYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 1;
Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
} else {
prevMonth = currentMonth - 1;
nextMonth = currentMonth + 1;
nextYear = yy;
prevYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
}
int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay;
Log.d(tag, "Week Day:" + currentWeekDay + " is "
+ getWeekDayAsString(currentWeekDay));
Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);
if (cal.isLeapYear(cal.get(Calendar.YEAR)))
if (mm == 2)
++daysInMonth;
else if (mm == 3)
++daysInPrevMonth;
// Trailing Month days
for (int i = 0; i < trailingSpaces; i++) {
Log.d(tag, "PREV MONTH:= " + prevMonth + " => " + getMonthAsString(prevMonth) + " " + String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i));
list.add(String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" + getMonthAsString(prevMonth) + "-" + prevYear);
}
// Current Month Days
for (int i = 1; i <= daysInMonth; i++) {
Log.d(currentMonthName, String.valueOf(i) + " "
+ getMonthAsString(currentMonth) + " " + yy);
if (i == getCurrentDayOfMonth()) {
list.add(String.valueOf(i) + "-BLUE" + "-"
+ getMonthAsString(currentMonth) + "-" + yy);
} else {
list.add(String.valueOf(i) + "-WHITE" + "-"
+ getMonthAsString(currentMonth) + "-" + yy);
}
}
// Leading Month days
for (int i = 0; i < list.size() % 7; i++) {
Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
list.add(String.valueOf(i + 1) + "-GREY" + "-" + getMonthAsString(nextMonth) + "-" + nextYear);
}
}
private HashMap<String, Integer> findNumberOfEventsPerMonth(int year,
int month) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
return map;
}
@Override
public void onClick(View view) {
String date_month_year = (String) view.getTag();
Log.e("Selected date", date_month_year);
try {
Date parsedDate = dateFormatter.parse(date_month_year);
Log.d(tag, "Parsed Date: " + parsedDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.calender_view_layout, parent, false);
}
// Get a reference to the Day gridcell
gridcell = (TextView) row.findViewById(R.id.calendar_day_grid_cell);
mRelative_Root = (RelativeLayout) row.findViewById(R.id.mRelative_Root);
gridcell.setOnClickListener(this);
TextView tvdot = (TextView) row.findViewById(R.id.cdot_tv);
// ACCOUNT FOR SPACING
Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
String[] day_color = list.get(position).split("-");
final String theday = day_color[0];
String themonth = day_color[2];
String theyear = day_color[3];
/*if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null))
{
if (eventsPerMonthMap.containsKey(theday))
{
num_events_per_day = (TextView) row
.findViewById(R.id.num_events_per_day);
Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
num_events_per_day.setText(numEvents.toString());
}
}*/
int monthPosition = 0;
for (int i = 0; i < months.length; i++) {
if (months[i].equals(themonth.trim())) {
monthPosition++;
break;
} else {
monthPosition++;
}
}
gridcell.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(test!=null)
{
test.onDateClick(theday);
dddddd =theday;
}
}
});
String date_for_dot = theday + "/" + monthPosition + "/" + theyear;
/* for(int i=0;i<model.size();i++)
{*/
/* String f=model.get(i).getJobDate();
if(f.equals(date_for_dot)){
dot_check_flag=true;
break;
}else{
dot_check_flag=false;
}*/
//Toast.makeText(_context,themonth+"\n"+date_for_dot+"\n"+dot_check_flag+"\n"+f,Toast.LENGTH_SHORT).show();
// }
// Set the Day GridCell
gridcell.setText(theday);
gridcell.setTag(theday + "-" + themonth + "-" + theyear);
Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-" + theyear);
if(dddddd != null){
if (theday.equalsIgnoreCase(dddddd)){
gridcell.setTextColor(_context.getResources().getColor(R.color.white));
gridcell.setBackgroundDrawable(_context.getResources().getDrawable(R.drawable.blue_circle_drawable));
if (!dot_check_flag)
tvdot.setBackgroundResource(R.color.white);
}else {
gridcell.setTextColor(_context.getResources().getColor(R.color.lightgray02));
if (!dot_check_flag)
tvdot.setBackgroundResource(R.color.white);
}
}
if(date_for_dot.equalsIgnoreCase(getdat)){
gridcell.setTextColor(_context.getResources().getColor(R.color.white));
gridcell.setBackgroundDrawable(_context.getResources().getDrawable(R.drawable.blue_circle_drawable));
if (!dot_check_flag)
tvdot.setBackgroundResource(R.color.white);
}
//22/8/2015
if (day_color[1].equals("GREY")) {
gridcell.setTextColor(_context.getResources().getColor(R.color.lightgray));
if (!dot_check_flag)
tvdot.setBackgroundResource(R.color.white);
}
if (day_color[1].equals("WHITE")) {
gridcell.setTextColor(_context.getResources().getColor(R.color.lightgray02));
if (!dot_check_flag)
tvdot.setBackgroundResource(R.color.white);
}
if (day_color[1].equals("BLUE")) {
gridcell.setTextColor(_context.getResources().getColor(R.color.white));
gridcell.setBackgroundDrawable(_context.getResources().getDrawable(R.drawable.blue_circle_drawable));
if (!dot_check_flag)
tvdot.setBackgroundResource(R.color.white);
}
return row;
}
public int getCurrentDayOfMonth() {
return currentDayOfMonth;
}
private void setCurrentDayOfMonth(int currentDayOfMonth) {
this.currentDayOfMonth = currentDayOfMonth;
}
public void setCurrentWeekDay(int currentWeekDay) {
this.currentWeekDay = currentWeekDay;
}
public int getCurrentWeekDay() {
return currentWeekDay;
}
}
Create calender_view_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:gravity="center"
android:id="@+id/mRelative_Root"
android:orientation="vertical" >
<TextView
android:id="@+id/calendar_day_grid_cell"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center"
android:text="8"
android:textSize="15dp"
android:textColor="#343663" >
</TextView>
<TextView
android:id="@+id/cdot_tv"
android:layout_width="3dp"
android:layout_below="@id/calendar_day_grid_cell"
android:layout_height="3dp"
android:layout_marginLeft="13dp"
android:gravity="center_vertical"
android:background="#343663"
>
</TextView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:gravity="center"
android:id="@+id/mRelative_Root"
android:orientation="vertical" >
<TextView
android:id="@+id/calendar_day_grid_cell"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center"
android:text="8"
android:textSize="15dp"
android:textColor="#343663" >
</TextView>
<TextView
android:id="@+id/cdot_tv"
android:layout_width="3dp"
android:layout_below="@id/calendar_day_grid_cell"
android:layout_height="3dp"
android:layout_marginLeft="13dp"
android:gravity="center_vertical"
android:background="#343663"
>
</TextView>
</RelativeLayout>
Create drawable
blue_circle_drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@color/blue"></solid>
<size android:width="1dp" android:height="1dp" ></size>
</shape>
No comments:
Post a Comment