I'm super-new in Java and Android Studio. I've started app with date functionality. I've created 3 buttons in app menu. One is button back, date, and button forward.
case 1. When you start the app, current date shows up on date button, when you click button back or forward that date on the middle button changes accordingly by one day.
case 2. When you click date button itself (for the first time) it pops out the date picker with the date from the button highlighted, you can change the date and it changes the date button accordingly from the picker.
case 3. But when I use buttons forward and back after the picker was out (once) date still changes on the button, but it does not highlights in the picker. It only highlights correctly first time around, it also highlights correctly when changing date via picker, but not via back and forward buttons.
What am I doing wrong (beside using deprecated methods? ;-)). Any input will be very appreciated.
//variables for date
int year_x,month_x,day_x;
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
MenuItem dynamicDate;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
dynamicDate = menu.findItem(R.id.action_date);
Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH) + 1;
day_x = cal.get(Calendar.DAY_OF_MONTH);
String currentDateString = (day_x<10?("0"+day_x):(day_x)) + "-" + (month_x<10?("0"+month_x):(month_x)) + "-" + year_x;
Date currentDate = null;
try {
currentDate = df.parse(currentDateString);
} catch (ParseException e) {
e.printStackTrace();
}
dynamicDate.setTitle(currentDateString);
return true;
}
public DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int year_xk, int month_xk, int day_xk) {
Calendar cal = Calendar.getInstance();
cal.set(year_xk, month_xk, day_xk);
Date dateAfterChange = cal.getTime();
String dateAfterChangeString = df.format(dateAfterChange);
dynamicDate.setTitle(dateAfterChangeString);
//Toast.makeText(MainActivity.this, "you clicked date", Toast.LENGTH_SHORT).show();
}
};
@Override
public Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 0473) {
String currentDateString = dynamicDate.getTitle().toString();
//DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date currentDate = null;
try {
currentDate = df.parse(currentDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
return new DatePickerDialog(this, myDateListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
}
return null;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_back) {
String currentDateString = dynamicDate.getTitle().toString();
//DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date currentDate = null;
try {
currentDate = df.parse(currentDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
cal.add(Calendar.DATE, -1);
Date dateDayBefore = cal.getTime();
String dateDayBeforeString = df.format(dateDayBefore);
dynamicDate.setTitle(dateDayBeforeString);
//return true;
}
if (id == R.id.action_date) {
showDialog(0473);
}
if (id == R.id.action_forward) {
String currentDateString = dynamicDate.getTitle().toString();
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date currentDate = null;
try {
currentDate = df.parse(currentDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
cal.add(Calendar.DATE, +1);
Date dateDayAfter = cal.getTime();
String dateDayAfterString = df.format(dateDayAfter);
dynamicDate.setTitle(dateDayAfterString);
//return true;
}
return super.onOptionsItemSelected(item);
}