I want to take the date from DatePicker

265 Views Asked by At

Well I am doing an app that has one activity and another class called MyPicker that extends DialogFragment and implements DatePickerDialog.OnDateSetListener

The thing is when user click in a button a dataPicker appear and I want to take that date from datePicker to a texfield that it is the activity.

How do you recommend I do that?

2

There are 2 best solutions below

0
On BEST ANSWER

If you are changing the date in date picker then you can do like this

datePicker.setOnDateChangeListner(new OnDateChangedListener() {
 public void onDateChanged(DatePicker view, int year1,
int monthOfYear1, int dayOfMonth1) {
year = year1;
monthOfYear = monthOfYear1;
dayOfMonth = dayOfMonth1;
}
}
);
1
On
public class MainActivity extends Activity implements OnClickListener {

    private Button mDateButton;

    private Calendar mCalen;
    private int day;
    private int month;
    private int year;

    private static final int DATE_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateButton = (Button) findViewById(R.id.date_button);
        mCalen = Calendar.getInstance();
        day = mCalen.get(Calendar.DAY_OF_MONTH);
        month = mCalen.get(Calendar.MONTH);
        year = mCalen.get(Calendar.YEAR);
        mDateButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DATE_PICKER_ID:
                return new DatePickerDialog(this, datePickerListener,
                        year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener =
            new DatePickerDialog.OnDateSetListener() {

                // while dialog box is closed, below method is called.
                public void onDateSet(DatePicker view, int selectedYear,
                        int selectedMonth, int selectedDay) {
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;

                    // Set the Date String in Button
                    mDateButton.setText(day + " / " + (month + 1) + " / " + year);
                }
            };

    @Override
    public void onClick(View v) {

        showDialog(DATE_PICKER_ID);

    }
}