Android : How to restrict date picker to start from today date in android

on Saturday, August 16, 2014


I am having twor textViews in my activity,It displays from and two dates,now i have put a datepicker dialog on both textView's click event,I want is,The from textView datepicker should only start with current date,and the "To textView" must start with value of "From textView",Means "To textView" value must be greater than "From"..Please help me ,My code is as below: code



public class AddBookingpopupActivity extends Activity {
Spinner spr_dest;
TextView tv_from, tv_to, tv_cancel, tv_add;
JSONObject jsenb;
ArrayList<HashMap<String, String>> dest;
DestAdapter adapter;
StringEntity se;
public static final String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
private ProgressDialog pDialog;
private Calendar cal;
private int dy;
String city, city_id;
private int month;
private int year;
int flag = 0;
String fn, ln, number, mob, src, email;
String reqUrl;
protected void onCreate(Bundle savedInstanceState) {
/*
* if (Build.VERSION.SDK_INT < 13) {
* setTheme(android.R.style.Theme_Holo_Light); } else {
* setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen); }
*/
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_new_book);
init();
tv_from.setOnClickListener(new OnClickListener() {

@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
flag = 1;
showDialog(0);
}
});
tv_to.setOnClickListener(new OnClickListener() {

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

flag = 2;
showDialog(0);
}
});
}
@SuppressLint("InlinedApi")
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
return new DatePickerDialog(this,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
datePickerListener, year, month, dy);

}

public DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
if (flag == 1) {
tv_from.setText(selectedYear + "/" + (selectedMonth + 1) + "/"
+ selectedDay);
} else if (flag == 2) {

tv_to.setText(selectedYear + "/" + (selectedMonth + 1) + "/"
+ selectedDay);
}

}
};

void init() {

dest = new ArrayList<HashMap<String, String>>();
dest = Const.dest;
spr_dest = (Spinner) findViewById(R.id.spr_dest);
tv_from = (TextView) findViewById(R.id.tv_from);
tv_to = (TextView) findViewById(R.id.tv_to);
tv_cancel = (TextView) findViewById(R.id.tv_cancel);
tv_add = (TextView) findViewById(R.id.tv_add);
adapter = new DestAdapter(AddBookingpopupActivity.this, dest);
spr_dest.setAdapter(adapter);
cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH) + 1;
dy = cal.get(Calendar.DATE);
tv_to.setText(year + "/" + month + "/" + dy);
tv_from.setText(year + "/" + month + "/" + dy);
}

0 comments:

Post a Comment