2

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.

A. Badakhshan
  • 1,045
  • 7
  • 22
rodaxyz
  • 21
  • 3

2 Answers2

0
    //In your actiivty
String date=null;

public void setDate(String date){
    this.date=date;
    //set this date in your text View
}


   //In your fragment

Context context;
@Override
public void onAttach(Context context){
    this.context=context;
}

public void onDateSet(DatePicker view, int year, int month, int day){
    String date=//get Date over here
    ((TARForm)context).setDate(date)
}
Ashish Kumar
  • 374
  • 4
  • 11
0

You can try below code in your fragment inside onDateSet() method

public void onDateSet(DatePicker view, int year, int month, int day) {

        selectday = day;
        selectmonth = month+1;
        selectyear = year;
        date = selectday + "-" + selectmonth + "-" + selectyear;

        if(getActivity() instanceOf TARForm){
           ((TARForm)getActivity()).departuredate.setText(date);
        }
    }

and in your TARForm activity make public EditText departuredate;if your Activity and Fragment are in different folders

ShekharKG
  • 675
  • 6
  • 11