Android Datepicker Fragment - How to do something when the user sets a date?

Viewed 19549

I'm following the tutorial here: http://developer.android.com/guide/topics/ui/controls/pickers.html

I got things to work and the datepicker shows up. However, I am stuck on how to act on when the user sets the date.

I see they have this method:

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
}

But that does nothing if I put any code in it.

I'm not that familiar with Java so I'm not sure what additional code is needed in order to update a textview with the chosen date.

I did add this code:

private DatePickerDialog.OnDateSetListener mDateListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int month, int day) {
                updateDate(year, month, day);
            }
        };

public void updateDate(int year, int month, int day) {
    TextView bdate = (TextView)findViewById(R.id.birthDate);
    Date chosenDate = new Date(year, month, day);
    java.text.DateFormat dateFormat = DateFormat.getDateFormat(getApplicationContext());
    bdate.setText(dateFormat.format(chosenDate));
}

But that does nothing.

I did find this almost exact question: Get date from datepicker using dialogfragment

However in that solution wouldn't using (EditSessionActivity) limit the DatePickerFragment class to working with only the EditSessionActivity?

3 Answers

in my experience this code work perfectly!, I'm using win10 Android Studio 2.2...

import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;


public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);}

    public void datePicker(View view){
        DatePickerFragment fragment_date = new DatePickerFragment();
        fragment_date.show(getSupportFragmentManager(),"datePicker");}

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
         String day_x = ""+ day  ,month_x = ""+(month+1);
         if (day   < 9 ){ day_x   = "0" + day_x;}
         if (month < 9 ){ month_x = "0" + month_x;}
         ((EditText) findViewById(R.id.txtDate)).setText(day_x + "/" + month_x + "/" + year);}

    public static class DatePickerFragment extends DialogFragment {
        @Override
        public Dialog onCreateDialog (Bundle savedInstanceState){
            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() ,(DatePickerDialog.OnDateSetListener)this,day,month,year);}}

}

I've a "textView txtDate" and a "button with onclick = datePicker"

Related