I'm a complete beginner in android studio and I am following this tutorial to insert a date in my app from a DatePicker
widget. https://www.youtube.com/watch?v=33BFCdL0Di0
This funcionality exists in a fragment I have and not on my MainActivity . The problem is that the onDateSet method does not run when I click a date on my calendar .
My code :
DatePickerFragment.java
public class DatePickerFragment extends DialogFragment {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
return new DatePickerDialog(getContext(),(DatePickerDialog.OnDateSetListener) getView() ,year,month,day);
}
}
StatisticsFragment.java //where I insert a date
public class StatisticsFragment extends Fragment implements DatePickerDialog.OnDateSetListener{
private FragmentStatisticsBinding binding;
private View root;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentStatisticsBinding.inflate(inflater, container, false);
root = binding.getRoot();
EditText fromInput = (EditText) root.findViewById(R.id.inputFrom);
fromInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(requireActivity().getSupportFragmentManager(), "date picker");
}
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onDateSet(DatePicker view , int year , int month , int dayOfMonth){
System.out.println("I am in "); // does not enter
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,month);
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
TextView fromText = root.findViewById(R.id.from);
fromText.setText(currentDateString);
}
}
frament_statistics.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="56dp"
android:text="From :"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewStats" />
<EditText
android:id="@+id/inputTo"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="16dp"
android:hint="Enter date"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/inputFrom"
app:layout_constraintTop_toBottomOf="@+id/to" />
<TextView
android:id="@+id/viewStats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="64dp"
android:text="View vaccination statistics for specific dates : "
android:textSize="17sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I would appreciate your help
I little modified your code and make it work. Please go through it.
StatisticsFragment.java
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import java.text.DateFormat;
import java.util.Calendar;
public class StatisticsFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
private FragmentStatisticsBinding binding;
private View root;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentStatisticsBinding.inflate(inflater, container, false);
root = binding.getRoot();
binding.inputTo.setOnClickListener(v -> {
DialogFragment datePicker = new DatePickerFragment(StatisticsFragment.this);
datePicker.show(requireActivity().getSupportFragmentManager(), "date picker");
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
System.out.println("I am in "); // does not enter
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
binding.from.setText("From : " + currentDateString);
}
}
DatePickerFragment.java
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog.OnDateSetListener listener;
public DatePickerFragment(DatePickerDialog.OnDateSetListener listener) {
this.listener = listener;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getContext(), listener, year, month, day);
}
}
fragment_statistics.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">
<TextView
android:id="@+id/from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="56dp"
android:text="From :"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewStats" />
<EditText
android:id="@+id/inputTo"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="16dp"
android:focusable="false"
android:hint="Select date"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/from" />
<TextView
android:id="@+id/viewStats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="64dp"
android:text="View vaccination statistics for specific dates : "
android:textSize="17sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Hope this will be helpful.