Android Custom Calendar
import java.text.DateFormatSymbols;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author Sudhi.S
*
*/
public class UDSCalender extends Dialog implements
android.view.View.OnClickListener {
private TextView txt1 = null;
private TextView txt = null;
private GridView gd = null;
private Calendar calendar = null;
public UDSCalender(Context context) {
super(context, android.R.style.Theme_Panel);
calendar = Calendar.getInstance();
LinearLayout mreal = new LinearLayout(getContext());
LinearLayout.LayoutParams mrealp = new LinearLayout.LayoutParams(-1, -2);
mreal.setBackgroundResource(R.drawable.abs__dialog_full_holo_light);
mreal.setOrientation(LinearLayout.VERTICAL);
View vo = new View(getContext());
vo.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo);
TextView txt = new TextView(getContext());
txt.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
txt.setText("Calendar");
txt.setPadding(0, 5, 0, 5);
txt.setTextColor(Color.BLACK);
txt.setGravity(Gravity.CENTER);
mreal.addView(txt);
View vo1 = new View(getContext());
vo1.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo1.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo1);
addButton(mreal);
View vo3 = new View(getContext());
vo3.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo3.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo3);
addCalender(mreal);
View vo2 = new View(getContext());
vo2.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo2.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo2);
this.setCancelable(true);
this.addContentView(mreal, mrealp);
}
ArrayList<Date> dater=null;
public void setArrayList(ArrayList<Date> dater)
{
this. dater=dater;
}
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month];
}
private void addButton(LinearLayout mreal) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
LinearLayout ll = new LinearLayout(getContext());
ll.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView img = new ImageView(getContext());
img.setImageResource(android.R.drawable.btn_minus);
img.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img.setOnClickListener(this);
img.setTag("0");
ll.addView(img);
txt = new TextView(getContext());
txt.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1));
txt.setText(year + "");
txt.setTextColor(Color.BLACK);
txt.setGravity(Gravity.CENTER);
ll.addView(txt);
ImageView img1 = new ImageView(getContext());
img1.setImageResource(android.R.drawable.btn_plus);
img1.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img1.setTag("1");
img1.setOnClickListener(this);
ll.addView(img1);
ImageView img3 = new ImageView(getContext());
img3.setImageResource(android.R.drawable.btn_minus);
img3.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img3.setTag("2");
img3.setOnClickListener(this);
ll.addView(img3);
txt1 = new TextView(getContext());
txt1.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1));
txt1.setText(getMonth(month));
txt1.setTag(month);
txt1.setTextColor(Color.BLACK);
txt1.setGravity(Gravity.CENTER);
ll.addView(txt1);
ImageView img4 = new ImageView(getContext());
img4.setImageResource(android.R.drawable.btn_plus);
img4.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img4.setTag("3");
img4.setOnClickListener(this);
ll.addView(img4);
mreal.addView(ll);
}
private void addCalender(LinearLayout mreal) {
gd = new GridView(getContext());
gd.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
gd.setNumColumns(7);
gd.setCacheColorHint(android.R.color.transparent);
ArrayList<String> dat = getarrayList((calendar.get(Calendar.MONTH)),
calendar.get(Calendar.YEAR));
newmyadapter ad = setadap(dat);
gd.setAdapter(ad);
mreal.addView(gd);
}
private newmyadapter setadap(ArrayList<String> dat) {
newmyadapter ad = new newmyadapter( dat);
return ad;
}
private ArrayList<String> getarrayList(int month, int year) {
ArrayList<String> dat = new ArrayList<String>();
dat.add("Sun");
dat.add("Mon");
dat.add("Tue");
dat.add("Wed");
dat.add("Thu");
dat.add("Fri");
dat.add("Sat");
Calendar mycal =Calendar.getInstance();
mycal.set(year, month, 1);
System.out.println(year+"-"+month);
int days= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
try {
Date date1 = (new GregorianCalendar(year, month, 1)).getTime();
Format formatter = new SimpleDateFormat("EEEE");
String s = formatter.format(date1);
int h = 0;
String ne = s.toString().substring(0, 3);
System.out.println("ne" + ne);
if (ne.equalsIgnoreCase("sun")) {
h = 0;
} else if (ne.equalsIgnoreCase("mon")) {
h = 1;
} else if (ne.equalsIgnoreCase("tue")) {
h = 2;
} else if (ne.equalsIgnoreCase("wed")) {
h = 3;
} else if (ne.equalsIgnoreCase("thu")) {
h = 4;
} else if (ne.equalsIgnoreCase("fri")) {
h = 5;
} else if (ne.equalsIgnoreCase("sat")) {
h = 6;
}
System.out.println(h + "h");
for (int j = 0; j < h; j++) {
dat.add(".");
System.out.println(j + "h...");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(days);
for (int i = 1; i < days + 1; i++) {
dat.add(i + "");
}
return dat;
}
@Override
public void onClick(View v) {
int tag = Integer.parseInt(v.getTag().toString());
int next;
switch (tag) {
case 0:
next = Integer.parseInt(txt.getText().toString().trim()) - 1;
txt.setText(next + "");
gd.setAdapter(setadap(getarrayList(
Integer.parseInt(txt1.getTag().toString().trim()), next)));
break;
case 1:
next = Integer.parseInt(txt.getText().toString().trim()) + 1;
txt.setText(next + "");
gd.setAdapter(setadap(getarrayList(
Integer.parseInt(txt1.getTag().toString().trim()), next)));
break;
case 2:
next = (Integer.parseInt(txt1.getTag().toString().trim()) == 1) ? Integer
.parseInt(txt1.getTag().toString().trim()) : Integer
.parseInt(txt1.getTag().toString().trim()) - 1;
txt1.setText(getMonth(next));
txt1.setTag(next);
gd.setAdapter(setadap(getarrayList(next, next)));
break;
case 3:
next = (Integer.parseInt(txt1.getTag().toString().trim()) == 12) ? Integer
.parseInt(txt1.getTag().toString().trim()) : Integer
.parseInt(txt1.getTag().toString().trim()) + 1;
txt1.setText(getMonth(next));
txt1.setTag(next);
System.out.println("top" + next + "-"
+ Integer.parseInt(txt.getText().toString().trim()));
gd.setAdapter(setadap(getarrayList(next,
Integer.parseInt(txt.getText().toString().trim()))));
break;
default:
break;
}
}
public class newmyadapter extends BaseAdapter
{
protected ArrayList<String> data=null;
public newmyadapter(ArrayList<String> data)
{
this.data=data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView txt=new TextView(getContext());
txt.setText(data.get(position));
txt.setGravity(Gravity.CENTER);
txt.setTextColor(Color.BLACK);
String datee=data.get(position)+"-"+Integer.parseInt(txt1.getTag().toString().trim())+"-"+txt.getText().toString().trim();
if(dater!=null)
{
if(dater.contains(datee))
{
txt.setBackgroundColor(Color.RED);
txt.setClickable(false);
}else
{
txt.setBackgroundColor(Color.GREEN);
txt.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
return txt;
}
}
}
No comments:
Post a Comment