0

I am following this link to create a DatePickerDialog and copy the selected date to an Editext field. However, I could not manage to understand how to do that, as whatever I do in onDateSet method, it is unrecognizable. If anybody kindly help.

MAIN XML:

<EditText
                android:id="@+id/add_datepurchased"
                android:inputType="date"
                android:onClick="showdate" />

Main Class:

EditText datpurchased;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_entry);
        // Show the Up button in the action bar.
        setupActionBar();

        datpurchased = (EditText) findViewById(R.id.add_datepurchased);
    }
public void showdate(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

DatePickerFragment Class:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @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(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day){
        EditText datpurchased = (EditText) findViewById(R.id.add_datepurchased); // GIVES ERROR
        datpurchased.setText(year+"-"+(month+1)+"-"+day);
    }
}
abdfahim
  • 2,523
  • 10
  • 35
  • 71
  • What happens if you run this? What error you got? – Subramanian Ramsundaram Nov 10 '13 at 15:04
  • I can't run as DatePickerFragment Class is throwing an error "The method findViewById(int) is undefined for the type DatePickerFragment" – abdfahim Nov 10 '13 at 15:06
  • Try this : EditText datpurchased = (EditText)view findViewById(R.id.add_datepurchased); – Manishika Nov 10 '13 at 15:08
  • it's showing same error ... – abdfahim Nov 10 '13 at 15:13
  • 1
    @AbdullahFahim check this http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment if it helps. use the first method. there date is set to textview.in your case it would be edittext – Raghunandan Nov 10 '13 at 15:19
  • @AbdullahFahim `findViewById` looks for a view with the id mentioned in the current inflated layout. http://developer.android.com/reference/android/app/Activity.html#findViewById(int) and this http://developer.android.com/reference/android/view/View.html#findViewById(int). You are not infaling a layout and you can use `findViewById` like that – Raghunandan Nov 10 '13 at 15:24
  • awesome (http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment), though I am having tough time understanding all functions there ... early days for me in Java ... btw, I can accept if you reply. – abdfahim Nov 10 '13 at 15:38

1 Answers1

0

You should intialize EditText in onCreate()