Craig Gallagher Craig Gallagher - 7 months ago 22
Java Question

Adding date from a calendar into EditText in a different class

I have made an application where a user can select a date on a calendar in my

Calendar.java
activity once the user selects this date I want it to put this date in an
EditText
field in a separate activity called AddDeadline. Currently I have a calendar and once the user selects that date it brings them to the separate activity called
AddDeadline.java
and brings the date up as a toaster. Is there a way that when the user selects a date that it will put that date into my
EditText
in my AddDeadline.java activity field.

My code is as fallows

Calendar.java

CalendarView calendar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calender);


calendar = (CalendarView) findViewById(R.id.calendarView);
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Toast.makeText(getApplicationContext(), dayOfMonth+ "/" + month + "/" + year, Toast.LENGTH_LONG).show();
startActivity(new Intent(Calender.this, AddDeadline.class));
}

});
}


AddDeadline.java

public EditText editDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_deadline);

editDate = (EditText)findViewById(R.id.editText3);


final EditText edate = (EditText) findViewById(R.id.editText3);
edate.setOnClickListener(new View.OnClickListener()

{

@Override
public void onClick (View v){
// TODO Auto-generated method stub
//To show current date in the datepicker
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(AddDeadline.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
selectedmonth = selectedmonth + 1;
edate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select Date");
mDatePicker.show();
}
}

);
}

Answer

You can do put data to intent like

CalendarView calendar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calender);


    calendar = (CalendarView) findViewById(R.id.calendarView);
    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener()  {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
            Toast.makeText(getApplicationContext(), dayOfMonth+ "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Calender.this, AddDeadline.class)
             intent.putExtra("date", dayOfMonth+ "/" + month + "/" + year);
            startActivity(intent);
        }

    });
}

get date in AddDeadline class by

String data = getIntent().getExtras().getString("date");

like

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_deadline);
    String data = getIntent().getExtras().getString("date");

    editDate = (EditText)findViewById(R.id.editText3);
editDate.setText(date);


}
Comments