jason jason - 5 months ago 32
Android Question

How to get the value from DatePicker on Android

I have this DatePicker code, I can see the DatePicker and pick a date but nothing happens. How can I get the date and use it, probably show to the user?
Here is my DatePicker code :

private EditText et_tarih;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
et_tarih = (EditText) view.findViewById(R.id.et_tarih);


if(et_tarih != null)
{
et_tarih.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){

DialogFragment newFragment = new IsActivity.DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
return true;
}
return false;
}
});
}

}

Answer

Code for Pick Date

//         **ON Create**

    edpicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");

}
    });



  //In class call this

 public static class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm+1, dd);
    }
    public void populateSetDate(int year, int month, int day) {


        edpicker.setText(day+"-"+month+"-"+year);
    }

}
Comments