Hi I am new to Android and Java, and I am writing a Date Picker Fragment class which extends DialogFragment
and implements DatePickerDialog.OnDateSetListener
due to this I have to declare method public void onDateSet(DatePicker view, int year, int month, int day)
, in which I am setting my new date in a public
String
variable.
Is it possible I can use this public String
variable in my main activity to show the value in a TextView
? If yes How?
My Main Activity code is:
public class TARForm extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
EditText departuredate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tarform);
departuredate = (EditText) findViewById(R.id.departure_date);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
}
And DatePickerFragment
Code is:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
int selectyear, selectmonth, selectday;
public String date;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
selectday = day;
selectmonth = month+1;
selectyear = year;
date = selectday + "-" + selectmonth + "-" + selectyear;
}
}
If you can guide with this information, how I can get the "date" into MainActivity departuredate? Thanks.