0
votes

I have a question. I created a DatePickerDialog in Android Studio, but when the Datepickerdialog is displayed and I want to add a date, it doesn't work. I would like to see the DialogPicker when I press the button and thus change the date, but nothing appears in the text view. My code is down here. It would also be nice if someone told me how to make the date in the order of month, day, year

Fragmentclass:

public class Fragment extends Fragment implements DatePickerDialog.OnDateSetListener {

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    calendar = Calendar.getInstance();
    Button button = (Button) getView().findViewById(R.id.change);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment datepicker = new DatePickerFragment();
            datepicker.show(getFragmentManager(),  "date picker");
        }
    });
}


@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    String currentime = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
    TextView textView = getView().findViewById(R.id.date);
    textView.setText(currentime);
}

}

Class of DatePicker: public class DatePickerFragment extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    return new DatePickerDialog(getActivity(),(DatePickerDialog.OnDateSetListener) getTargetFragment(),
            year, month, day);
}

}

1

1 Answers

0
votes

fragment_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    orientation="Vertical"
    tools:context=".MainFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Date"
        android:id="@+id/Date"
        android:layout_marginTop="50dp"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"/>

        <Button
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Select Date"
        />
    </LinearLayout>

MainFragment.java

public class MainFragment extends Fragment {
    private static final String TAG = "MainFragment";

    private TextView date;
    private Button mDisplayDate;
    private DatePickerDialog.OnDateSetListener mDateSetListener;

   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment, container, false);

    mDisplayDate=view.findViewById(R.id.tvDate);
    date=view.findViewById(R.id.Date);
    mDisplayDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(
                    MainActivity.this,
                    android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                    mDateSetListener,
                    year,month,day);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();
        }
    });

    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1;
            Log.d(TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);

            String date = month + "/" + day + "/" + year;
            date.setText(date);
        }
    };



  return view;
   }
}