CalendarView usage in Fragment

43 Views Asked by At

I am creating an app where I want to display a calendar and below specific statistics for each selected day, but for now I am trying to display the selected day in a TextView (and a Toast just to be sure).

If I start an empty project and run my code in MainActivity it works just fine but when I try do adapt it to my CalendarFragment it does not work. The calendar is shown but nothing happens when the day is selected.

This is the MainActivity.java where the code runs perfectly:

public class MainActivity extends AppCompatActivity {

    CalendarView calendarView;
    Calendar calendar;

    TextView textViewSelectedDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        calendarView = findViewById(R.id.calendarView);
        textViewSelectedDate = findViewById(R.id.textViewSelectedDate);
        calendar = Calendar.getInstance();


        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {

                Toast.makeText(MainActivity.this, day + "/" + (month + 1) + "/" + year,Toast.LENGTH_SHORT).show();

                String selectedDate = day + "/" + (month + 1) + "/" + year;
                textViewSelectedDate.setText("Selected Date: " + selectedDate);
            }
        });
    }
}

And this is my CalendarFragment:

public class CalendarFragment extends Fragment {

    CalendarView calendarView;
    Calendar calendar;
    TextView textViewSelectedDate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_calendar, container, false);

        calendarView = view.findViewById(R.id.calendarView);
        textViewSelectedDate = view.findViewById(R.id.textViewSelectedDate);
        calendar = Calendar.getInstance();

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {

                Toast.makeText(requireContext(), day + "/" + (month + 1) + "/" + year, Toast.LENGTH_SHORT).show();

                String selectedDate = day + "/" + (month + 1) + "/" + year;
                textViewSelectedDate.setText("Selected Date: " + selectedDate);
            }
        });

        return view;
    }
}

Also my fragment_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CalendarFragment">

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:background="@color/black"
        android:dateTextAppearance="@style/CalenderViewDateCustomText"
        android:theme="@style/CalenderViewCustom"
        android:weekDayTextAppearance="@style/CalenderViewWeekCustomText" />

    <!-- TextView to display the selected date -->
    <TextView
        android:id="@+id/textViewSelectedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date: "
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="450dp"
        android:background="@color/black"
        android:textColor="@color/white"
       />
</FrameLayout>

I tried imports, other questions in stackoverflow and chatGPT and for no luck. If someone could give me some insight it would be much appreciated!

1

There are 1 best solutions below

1
André Gonçalves On

Suddenly it works, on another computer though.