public class CalendarActivity extends AppCompatActivity {
CompactCalendarView compactCalendarView;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM-YYYY", Locale.getDefault());
private SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
SimpleDateFormat sdf;
TextView tx_date, tx_today;
LinearLayout ly_detail;
LinearLayout ly_left, ly_right;
Calendar myCalendar;
ImageView im_back;
WebService webService;
ProgressDialog progressDialog;
String user_type;
int id;
Date c;
SimpleDateFormat df;
String formattedDate;
String[] dates = new String[0];
RecyclerView recyclerView;
TextView tx_item;
CalendarAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
init();
valid();
calendarlistener();
Setdate();
// Toast.makeText(getApplicationContext(),""+formattedDate,Toast.LENGTH_LONG).show();
new EventListAsy().execute("" + id, formattedDate, user_type);
new EventViewAsy().execute("" + id, formattedDate, user_type);
tx_item.setText(formattedDate + " No events available in this day");
ly_right.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
compactCalendarView.showCalendarWithAnimation();
compactCalendarView.showNextMonth();
}
});
ly_left.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
compactCalendarView.showCalendarWithAnimation();
compactCalendarView.showPreviousMonth();
}
});
tx_today.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Intent intent = new Intent(CalendarActivity.this, CalendarActivity.class);
startActivity(intent);
finish();
}
});
im_back.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
finish();
}
});
}
//variable initialization public void init() {
compactCalendarView = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
tx_date = (TextView) findViewById(R.id.text);
ly_left = (LinearLayout) findViewById(R.id.layout_left);
ly_right = (LinearLayout) findViewById(R.id.layout_right);
im_back = (ImageView) findViewById(R.id.image_back);
tx_today = (TextView) findViewById(R.id.text_today);
ly_detail = (LinearLayout) findViewById(R.id.layout_detail);
recyclerView = (RecyclerView) findViewById(R.id.list_recycleView);
tx_item = (TextView) findViewById(R.id.text_item);
}
//set value public void valid() {
id = getUserID();
user_type = getUserType();
progressDialog = new ProgressDialog(CalendarActivity.this, R.style.Dialog);
progressDialog.setCanceledOnTouchOutside(false);
}
//calendar method public void calendarlistener() {
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
@Override public void onDayClick(Date dateClicked) {
new EventViewAsy().execute("" + id, DateFormat.format(dateClicked), user_type);
tx_item.setText(DateFormat.format(dateClicked) + " No events available in this day");
}
@Override public void onMonthScroll(Date firstDayOfNewMonth) {
compactCalendarView.removeAllEvents();
tx_date.setText(simpleDateFormat.format(firstDayOfNewMonth));
new EventListAsy().execute("" + id, DateFormat.format(firstDayOfNewMonth), user_type);
new EventViewAsy().execute("" + id, DateFormat.format(firstDayOfNewMonth), user_type);
tx_item.setText(DateFormat.format(firstDayOfNewMonth) + " No events available in this day");
}
});
}
//get current date public void Setdate() {
c = Calendar.getInstance().getTime();
df = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = df.format(c);
}
class EventListAsy extends AsyncTask<String, Void, ArrayList<ScheduleJobBean>> {
@Override protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
webService = new WebService();
}
@Override protected ArrayList<ScheduleJobBean> doInBackground(String... params) {
return webService.getEventList(params[0], params[1], params[2]);
}
@Override protected void onPostExecute(final ArrayList<ScheduleJobBean> list) {
super.onPostExecute(list);
progressDialog.dismiss();
compactCalendarView.setUseThreeLetterAbbreviation(true);
sdf = new SimpleDateFormat("MMMM yyyy");
tx_date.setText(sdf.format(compactCalendarView.getFirstDayOfCurrentMonth()));
myCalendar = Calendar.getInstance();
for (int j = 0; j < list.size(); j++) {
String s1 = list.get(j).getDatetime();
dates = s1.split("-");
int mon = Integer.parseInt(dates[1]);
myCalendar.set(Calendar.YEAR, Integer.parseInt(dates[0]));
myCalendar.set(Calendar.MONTH, mon - 1);
myCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dates[2]));
Event event = new Event(Color.RED, myCalendar.getTimeInMillis(), "test");
compactCalendarView.addEvent(event);
}
}
}
class EventViewAsy extends AsyncTask<String, Void, ArrayList<ScheduleJobBean>> {
@Override protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
webService = new WebService();
}
@Override protected ArrayList<ScheduleJobBean> doInBackground(String... params) {
return webService.viewEvent(params[0], params[1], params[2]);
}
@Override protected void onPostExecute(final ArrayList<ScheduleJobBean> list) {
super.onPostExecute(list);
progressDialog.dismiss();
if (list.size() == 0) {
tx_item.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
tx_item.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
}
adapter = new CalendarAdapter(list, CalendarActivity.this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
}
//shared preferences get user id method private int getUserID() {
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
int user_id = prefs.getInt("User_id", 0);
return user_id;
}
//shared preferences get store type method private String getUserType() {
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String user_type = prefs.getString("User_type", null);
return user_type;
}
}
Step 4:
calendar_adpter_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/customername"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="@android:color/black"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="20dp"
android:background="@color/colorPrimary"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="@string/mobilenumber"
android:textColor="@android:color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:gravity="center|left"
android:orientation="horizontal">
<TextView
android:id="@+id/text_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:text="@string/mobile"
android:textColor="@android:color/black"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:text="@string/city"
android:textColor="@android:color/black"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center|left"
android:layout_weight="0.8">
<TextView
android:id="@+id/text_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:text="@string/city"
android:textStyle="bold"
android:textColor="@android:color/black"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:text="Date and Time"
android:textColor="@android:color/black"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center|left"
android:layout_weight="0.8">
<TextView
android:id="@+id/text_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:text="Date and Time"
android:textStyle="bold"
android:textColor="@android:color/black"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Step 5:
CalendarAdapter.java
public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.MyViewHolder> {
private List<ScheduleJobBean> jobList;
private Activity activity;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, phone,city,date;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.text_name);
phone = (TextView) view.findViewById(R.id.text_mobile);
city=(TextView)view.findViewById(R.id.text_city);
date=(TextView)view.findViewById(R.id.text_date);
}
}
public CalendarAdapter(List<ScheduleJobBean> moviesList, Activity activity) {
this.jobList = moviesList;
this.activity = activity;
}
@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.calendar_adapter_item, parent, false);
return new MyViewHolder(itemView);
}
@Override public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.name.setText(jobList.get(position).getName());
holder.phone.setText(jobList.get(position).getMobile());
holder.city.setText(jobList.get(position).getCity());
holder.date.setText(jobList.get(position).getDatetime());
}
@Override public int getItemCount() {
return jobList.size();
}
}
Step 6:
calendarbean.java
public class calendarbean{
private String name,mobile,city,state,datetime,message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
}
Step 7:
JsonParser.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
HttpURLConnection urlConnection;
public String parseGET(String GET_URL){
StringBuilder result = new StringBuilder();
try {
// URL url = new URL("https://api.github.com/users/dmnugent80/repos");
URL url = new URL(GET_URL);
urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
//urlConnection.set InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
}catch( Exception e) {
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return result.toString();
}
protected String sendPOST(String POST_URL,String POST_PARAMS) throws IOException {
String result=null;
StringBuffer response=null;
String USER_AGENT = "Mozilla/5.0";
// String POST_URL = "http://ss.urbansoftusa.com/api/authenticate";
// String POST_PARAMS = "email=issac@mirutechnologies.com&password=123456";
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
// For POST only - START con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Toast.makeText(getApplicationContext(),"response: "+response.toString(),Toast.LENGTH_LONG).show();
Log.d("response",response.toString());
// print result System.out.println(response.toString());
} else {
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getErrorStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("POST request not worked");
}
if(responseCode==200)
result=response.toString();
else if(responseCode==400 || responseCode==401)
result=response.toString();
else result=result;
return result;
}
public String sendPostRequest(String urlPost,JSONObject params) {
try {
URL url = new URL(urlPost); // here is your URL path
/*JSONObject postDataParams = new JSONObject();
postDataParams.put("name", "abc");
postDataParams.put("email", "abc@gmail.com");
Log.e("params",postDataParams.toString());*/
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.addRequestProperty("Content-Type","application/json");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
// writer.write(getPostDataString(params));
//for direct json obect writer.write(params.toString());
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new
InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
} else {
return new String("false : " + responseCode);
}
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}
Step 8
Webservice.java
public class WebService {
JSONParser jsonParser;
public WebService() {
jsonParser = new JSONParser();
}
//get event date public ArrayList<ScheduleJobBean> getEventList(String id, String date, String user_type) {
JSONObject jsonObject1, jsonObject;
JSONArray jsonArray;
ScheduleJobBean bean = new ScheduleJobBean();
ArrayList<ScheduleJobBean> list = new ArrayList<>();
String url = "Your URL";
try {
JSONObject postDataParams = new JSONObject();
//enter your json input methed
//it is sample
postDataParams.put("user_id", id);
postDataParams.put("date", date);
postDataParams.put("user_type", user_type);
jsonObject = new JSONObject(jsonParser.sendPostRequest(url, postDataParams));
jsonObject1 = jsonObject.getJSONObject("response_header");
// Log.i("Input" ,""+id+" "+date+" "+user_type);
// Log.i("ScheduleOutPut",jsonObject.toString());
bean.setMessage(jsonObject1.getString("return_msg"));
int val = jsonObject1.getInt("return_val");
if (val == 1) {
jsonArray = jsonObject.getJSONArray("response_data");
//Log.i("OutPut",jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++) {
bean = new ScheduleJobBean();
JSONObject jsonArrayJSONObject = jsonArray.getJSONObject(i);
//set value from bean
bean.setDatetime(jsonArrayJSONObject.getString("appointment_datetime"));
list.add(bean);
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//View Events public ArrayList<ScheduleJobBean> viewEvent(String id, String date, String user_type) {
JSONObject jsonObject1, jsonObject;
JSONArray jsonArray;
ScheduleJobBean bean = new ScheduleJobBean();
ArrayList<ScheduleJobBean> list = new ArrayList<>();
String url = "Your URL";
try {
JSONObject postDataParams = new JSONObject();
//enter your json input methed
//it is sample
postDataParams.put("user_id", id);
postDataParams.put("date", date);
postDataParams.put("user_type", user_type);
jsonObject = new JSONObject(jsonParser.sendPostRequest(url, postDataParams));
jsonObject1 = jsonObject.getJSONObject("response_header");
// Log.i("Input" ,""+id+" "+date+" "+user_type);
// Log.i("ScheduleOutPut",jsonObject.toString());
bean.setMessage(jsonObject1.getString("return_msg"));
int val = jsonObject1.getInt("return_val");
if (val == 1) {
jsonArray = jsonObject.getJSONArray("response_data");
//Log.i("OutPut",jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++) {
bean = new ScheduleJobBean();
JSONObject jsonArrayJSONObject = jsonArray.getJSONObject(i);
bean.setName(jsonArrayJSONObject.getString("customer_name"));
bean.setMobile(jsonArrayJSONObject.getString("customer_phone"));
bean.setCity(jsonArrayJSONObject.getString("customer_city"));
bean.setDatetime(jsonArrayJSONObject.getString("appointment_datetime"));
list.add(bean);
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
Step 4:
drawaple >>>>>>> layout_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<stroke android:width="1dp"
android:color="@color/colorPrimary"/>
<corners android:radius="5dp"/>
</shape>