DatePicker not showing - java

I am trying to create a popup date picker from a button (the button that pops it up also shows the date). However the popup isn't showing. When the button is pressed nothing happens.
The DatePicker needs to be casted from an xml since I want it to show as spinners not a calendar (and from what I've read after API 21 the only way to do that is in xml)
Here is my code for the onClickLlistner for the button:
final Button startDate = (Button)findViewById(R.id.startDate);
Calendar currentDate = Calendar.getInstance();
mYear = currentDate.get(Calendar.YEAR);
mMonth = currentDate.get(Calendar.MONTH);
int mDay = currentDate.get(Calendar.DAY_OF_MONTH);
startDate.setText(mMonth + "/" + mDay + "/" + mYear);
startDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//get date from button text
Calendar dateInButton = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
try {
dateInButton.setTime(sdf.parse(startDate.getText().toString()));
} catch (ParseException e) {
e.printStackTrace();
}
int pYear = dateInButton.get(Calendar.YEAR);
int pMonth = dateInButton.get(Calendar.MONTH);
int pDay = dateInButton.get(Calendar.DAY_OF_MONTH);
//display popup datepicker
final View dialogView = View.inflate(MyActivity.this, R.layout.date_picker, null);
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
datePicker.updateDate(pYear, pMonth, pDay);
datePicker.init(pYear, pMonth, pDay, new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
month++;
startDate.setText(month + "/" + day + "/" + year);
}
});
}
});

Related

I'm trying to filter fragments using dates

I'm trying to filter fragments that display income data that I pull from firebase. Displaying them is no problem. Filtering them however, is difficult to say the least. While the datepickers have no errors when I try filtering it
enter image description hereenter image description here
ViewIncomeFragment.java
//Filter Button
Button filterButton = v.findViewById(R.id.btnFilter);
filterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Filter the Income data
filterIncomeByDate(startDate, endDate);
// Show a dialog to get start and end date from the user
Calendar calendar2 = Calendar.getInstance();
DatePickerDialog datePickerDialog2 = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
if (endDate != null) {
endDate.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
}
if(validateDates() == false){
startDate.setText("StartDate");
endDate.setText("EndDate");
}
}
// handle the date set event
}, calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH));
datePickerDialog2.show();
Calendar calendar1 = Calendar.getInstance();
DatePickerDialog datePickerDialog1 = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
if (startDate != null) {
startDate.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
}
}
// handle the date set event
}, calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH));
datePickerDialog1.show();
}
});
return v;
//Method to filter the Income by date
public void filterIncomeByDate(TextView startDate, TextView endDate){
ArrayList<Income> filteredList = new ArrayList<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
Date start = dateFormat.parse(startDate.getText().toString());
Date end = dateFormat.parse(endDate.getText().toString());
for (Income income : incomeArrayList) {
Date incomeDate = dateFormat.parse(income.getDateIncome());
if (incomeDate.after(start) && incomeDate.before(end)) {
filteredList.add(income);
}
}
} catch (ParseException e) {
e.printStackTrace();
}
incomeArrayList.clear();
incomeArrayList.addAll(filteredList);
incomeAdapter.notifyDataSetChanged();
}
Only the fragments within the date range should be displaying

How can I set the specific selected date to min date in android date picker? (java)

I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.
I set the Data which user selected in Calander startDate and set this data as MinDate at end date.
I've tried in this way but it's not working :(
I can't use Mateiral Range Date Picker because I have to use specific theme.
Also I'm not good at programming so I'd appreciate it if you could explain it in detail.
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
final Calendar c = Calendar.getInstance();
startYear = c.get(Calendar.YEAR);
startMonth = c.get(Calendar.MONTH);
startDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month+1;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
}
}, startYear, startMonth, startDay);
dpd.show();
}
});
Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
endYear = c.get(Calendar.YEAR);
endMonth = c.get(Calendar.MONTH);
endDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
endYear = year;
endMonth = month;
endDay = dayOfMonth;
ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
}
}, endYear,endMonth, endDay);
dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
dpd.show();
}
});
This code:
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
Should be inside
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()
So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog

How to set "todate picker" selected date as maximum date of "from datepicker" date

I have two date picker From and To, here i can set From datepicker value as minimum date of to datepicker.
Now my requirement is ,if i select To datepicker date, i want to set it as maximum date of From datepicker date. I tried one solution, but its not working in my case.
private String fromDate;
private DatePickerDialog fromDatePicker;
private DatePickerDialog.OnDateSetListener fromDatePickerListener;
private Calendar fromDateCalendar;
private String toDate;
private DatePickerDialog toDatePicker;
private DatePickerDialog.OnDateSetListener toDatePickerListener;
private Calendar toDateCalendar;
Fromdatepicker OnDateSetListener method
fromDateCalendar = Calendar.getInstance();
fromDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
fromDateCalendar.set(Calendar.YEAR, year);
fromDateCalendar.set(Calendar.MONTH, monthOfYear);
fromDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
fromDate = fromDateCalendar.get(Calendar.YEAR) + "-" +
(fromDateCalendar.get(Calendar.MONTH)+1) + "-" +
fromDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewFrom.setText(fromDate);
toDatePicker.getDatePicker().setMinDate(fromDateCalendar.getTimeInMillis());
}
};
To datepicker OnDateSetListener method
toDateCalendar = Calendar.getInstance();
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewTo.setText(toDate);
todatepicker.getDatePicker().setMinDate(-2208902400000L);
fromdatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());
}
};
toDatePicker = new DatePickerDialog(getContext(),toDatePickerListener,
toDateCalendar.get(Calendar.YEAR), toDateCalendar.get(Calendar.MONTH),
toDateCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePicker = new DatePickerDialog(getContext(),fromDatePickerListener,
fromDateCalendar.get(Calendar.YEAR), fromDateCalendar.get(Calendar.MONTH),
fromDateCalendar.get(Calendar.DAY_OF_MONTH));
Textview listner for call datepicker dialog
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fromDatePicker.show();
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toDatePicker.show();
}
});
I tried
fromDatePicker.getDatePicker().setMaxDate(toDateCalendar.get‌​TimeInMillis());
in onDateSet method of toDatePickerListener, but its not
working
I have tried the following way,Hope it may help you.
private String selectedToDate = "";
private String selectedFromDate = "";
private Calendar calToDate, calFromDate;
Write this code in onCreate() method
calFromDate = convertToCalender(selectedFromDate);
calToDate = convertToCalender(selectedToDate);
Todate and FromDate TextView Clicks
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showFromDatePicker(fromdateListener, calFromDate, calToDate);
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showToDatePicker(todateListener, calToDate, calFromDate);
}
});
toDate and fromDate Listener
public DatePickerDialog.OnDateSetListener fromdateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calFromDate.set(year, monthOfYear, dayOfMonth);
selectedFromDate = returnFormattedDate(calFromDate.getTimeInMillis());
textviewFrom.setText(selectedFromDate);
}
};
DatePickerDialog.OnDateSetListener todateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calToDate.set(year, monthOfYear, dayOfMonth);
selectedToDate = returnFormattedDate(calToDate.getTimeInMillis());
textviewTo.setText(selectedToDate);
}
};
private String returnFormattedDate(long timeInMilliSec) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
return dateFormat.format(new Date(timeInMilliSec));
}
Methods to show DatePicker
private void showFromDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar minDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
if (minDateCalender != null) {
dialog.getDatePicker().setMinDate(minDateCalender.getTimeInMillis());
}
dialog.show();
}
private void showToDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar maxDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMaxDate(maxDateCalender.getTimeInMillis());
dialog.show();
}
Method to convert string date to Calender
private Calendar convertToCalender(String date) {
Calendar mCalender = Calendar.getInstance();
if (date == null) {
return mCalender;
} else {
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date returnDate = formatter.parse(date);
mCalender = Calendar.getInstance();
mCalender.setTime(returnDate);
return mCalender;
} catch (ParseException e) {
e.printStackTrace();
return mCalender;
}
}
}
If you are are using DatePicker widget in xml the following will work .
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
fromdatePicker.setMaxDate(toDateCalendar.getTimeInMillis());
//etToDate.setText(toDate);
}
};
Or if you opening DatePickerDialog oen after another . Then you should save the previously selected date and pass it to next DatePickerDialog before calling show.
pickerDialog.getDatePicker().setMaxDate(maxTime);// Set Max and min here
pickerDialog.show();

How to get radio button pop up when two date pickers have same date?

I am working on a simple app in which i have taken two date pickers they subtract two dates and display the result. But i want that if the two dates are same like 06/04/2017 then two radio button should pop up for selecting full day and half day as soon as it calculates that the two dates are same.
how to do that?
public class Leave extends AppCompatActivity {
TextView date;
private DatePickerDialog datePickerDialog;
TextView date2;
//TextView setDay;
private DatePickerDialog datePickerDialog2;
TextView no_of_days;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
date = (TextView) findViewById(R.id.date);
date2 = (TextView) findViewById(R.id.date2);
//setDay = (TextView) findViewById(R.id.setDay);
no_of_days = (TextView) findViewById(R.id.no_of_days);
// initiate the date picker and a button
date = (TextView) findViewById(R.id.date);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
// initiate the date picker and a button
date2 = (TextView) findViewById(R.id.date2);
// perform click event on edit text
date2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog2 = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date2.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog2.show();
}
});
no_of_days.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String date01 = date.getText().toString();
String date02 = date2.getText().toString();
try {
Date d = format.parse(date01);
Date d1 = format.parse(date02);
getDifferenceDays(d, d1);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.leave_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
public class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
SpinnerActivity() throws ParseException {
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
}
private void setupRadios() {
RadioButton radio_full = (RadioButton) findViewById(R.id.radio_full);
RadioButton radio_Half = (RadioButton) findViewById(R.id.radio_Half);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioleave);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int buttonId = radioGroup.getCheckedRadioButtonId();
switch (buttonId) {
case R.id.radio_full:
Toast.makeText(getApplicationContext(), "You have selected Full Day Leave", Toast.LENGTH_SHORT).show();
break;
case R.id.radio_Half:
Toast.makeText(getApplicationContext(), "You have selected Half Day Leave", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
public void getDifferenceDays(Date d1, Date d2) {
int daysdiff = 0;
long diff = d2.getTime() - d1.getTime();
long diffDays = diff / (24 * 60 * 60 * 1000) + 1;
daysdiff = (int) diffDays;
no_of_days.setText(Integer.toString(daysdiff));
System.out.println("day count=>" + daysdiff);
}
}
Compare the dates whenever any date is selected from any one of the date picker's as follows :
public class Leave extends AppCompatActivity {
TextView date;
private DatePickerDialog datePickerDialog;
TextView date2;
//TextView setDay;
private DatePickerDialog datePickerDialog2;
TextView no_of_days;
String date1 = "", date2 = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
date = (TextView) findViewById(R.id.date);
date2 = (TextView) findViewById(R.id.date2);
//setDay = (TextView) findViewById(R.id.setDay);
no_of_days = (TextView) findViewById(R.id.no_of_days);
RadioButton radio_full = (RadioButton) findViewById(R.id.radio_full);
RadioButton radio_Half = (RadioButton) findViewById(R.id.radio_Half);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioleave);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int buttonId = radioGroup.getCheckedRadioButtonId();
switch (buttonId) {
case R.id.radio_full:
Toast.makeText(getApplicationContext(), "You have selected Full Day Leave", Toast.LENGTH_SHORT).show();
break;
case R.id.radio_Half:
Toast.makeText(getApplicationContext(), "You have selected Half Day Leave", Toast.LENGTH_SHORT).show();
break;
}
}
});
// initially hide the radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
// initiate the date picker and a button
date = (TextView) findViewById(R.id.date);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
// assign the value to date1
date1 = dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year;
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
// check if date2 has been set and compare it with date1
if(!TextUtils.isEmpty(date2)) {
// get difference
getDifferenceDays(date, date2);
if(date1.equals(date2)) {
// pop up radio button
radio_full.setVisibility(View.VISIBLE);
radio_Half.setVisibility(View.VISIBLE);
} else {
// hide radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
}
}
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
// initiate the date picker and a button
date2 = (TextView) findViewById(R.id.date2);
// perform click event on edit text
date2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog2 = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
// assign the value to date2
date2 = dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year;
date2.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
// check if date2 has been set and compare it with date1
if(!TextUtils.isEmpty(date1)) {
// get difference
getDifferenceDays(date, date2);
if(date1.equals(date2)) {
// pop up radio button
radio_full.setVisibility(View.VISIBLE);
radio_Half.setVisibility(View.VISIBLE);
} else {
// hide radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
}
}
}
}, mYear, mMonth, mDay);
datePickerDialog2.show();
}
});
no_of_days.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String date01 = date.getText().toString();
String date02 = date2.getText().toString();
try {
Date d = format.parse(date01);
Date d1 = format.parse(date02);
getDifferenceDays(d, d1);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.leave_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}

Need checkbox to allow button to be pressed - IF not working

I made a button to open a date and time picker, but a CheckBox must be checked in order to activate my button. Without the checkbox, it works fine, with the chekbox checked or unchecked, it's not opening the date picker. Can you tell me why?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pick_date = (Button) findViewById(R.id.pick_date);
tv_result = (TextView) findViewById(R.id.tvdate_result);
CheckBox fixed_date_flag = (CheckBox) findViewById(R.id.fixed_date);
final boolean checked = (fixed_date_flag).isChecked();
if(checked){
pick_date.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(NewActivity.this, NewActivity.this, year, month, day);
datePickerDialog.show();
}
});}
}
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
yearFinal = year;
monthFinal = month + 1;
dayFinal = dayOfMonth;
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(NewActivity.this, NewActivity.this, hour, minute, DateFormat.is24HourFormat(this));
timePickerDialog.show();
}
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
hourFinal = hourOfDay;
minuteFinal = minute;
tv_result.setText(
"year: " + yearFinal + "\n" +
"month: " + monthFinal + "\n" +
"day: " + dayFinal + "\n" +
"hour: " + hourFinal + "\n" +
"minute: " + minuteFinal);
}
}
your problem is very simple:
When creating the activity, your are checking if the checkbox is checked. If this value is true (but is not true) you add a click listener to the datePicker. So, the picker does not have a click listener! This means that when you "tap" the picker, it has no listener added, even if the checkbox is checked.
You must move the if statement inside the onClick method, like this:
EDITED:
pick_date.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
if((fixed_date_flag).isChecked()){
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(NewActivity.this, NewActivity.this, year, month, day);
datePickerDialog.show();
}
}
});
Nevertheless, I think is a much better option to create a separated cass that implements OnClickListener interface, so in the constructor you can initialize the datepicker, instanciated as a privated variable of this new class, and on the onClick method you only call to .show() method (if the checkbox is checked, obbiusly)
Hope it helps!

Categories

Resources