java – Fragment overlay

Question:

Faced a problem: when clicking on a calendar day, a new fragment should be called in which an event should be added. After adding and pressing submit, we return to the calendar snippet. So: the callback goes through without problems, and when you click on the date, the event fragment appears on top of the calendar fragment.

Here is a class with a calendar:

public class MainActivityFragment extends Fragment {

    FrameLayout calendarFL;
    CalendarView calendar;

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(final LayoutInflater inflater,
                             final ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_calendar, container, false);
        calendarFL = (FrameLayout)v.findViewById(R.id.calendarFL);
        calendar = (CalendarView) v.findViewById(R.id.calendarView);
        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                                            int dayOfMonth) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Fragment fragment = new EventsFragment();
                ft.replace(R.id.eventFL,fragment);
                ft.addToBackStack(null);
                ft.commit();
            }
        });
        return v;    
    }
}

Below is the class of the event fragment (for now, I only implement the transition between fragments):

public class EventsFragment extends Fragment {

    Button submit;
    FrameLayout eventFL;
    TextInputLayout eventDetails;
    EditText eventDet;
    CheckBox addAlarm;
    Spinner events;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_events,container,false);
        eventFL = (FrameLayout)v.findViewById(R.id.eventFL);
        submit = (Button)v.findViewById(R.id.submitButton);
        eventDetails = (TextInputLayout) v.findViewById(R.id.eventdetTIL);
        eventDet = (EditText)v.findViewById(R.id.eventdetailET);
        addAlarm = (CheckBox)v.findViewById(R.id.alarmCKB);
        events = (Spinner)v.findViewById(R.id.eventSpinner);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Fragment fragment = new MainActivityFragment();
                ft.replace(R.id.calendarFL,fragment);
                ft.addToBackStack(null);
                ft.commit();
            }
        });
        return v;
    }
}

Calendar layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/calendarFL">

    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/calendarView" />
</FrameLayout>

Event layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/eventFL">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/topRL">

            <CheckBox
                android:text="@string/add_alarm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/alarmCKB"
                android:checked="false"
                android:gravity="top|left"
                android:layout_alignParentLeft="false"
                android:layout_alignParentRight="true"
                android:paddingLeft="16dp"
                android:padding="16dp"
                android:layout_marginRight="16dp" />

            <Spinner
                android:layout_height="wrap_content"
                android:id="@+id/eventSpinner"
                android:layout_marginTop="16dp"
                android:layout_width="200dp"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/bottomRL"
            android:layout_below="@+id/topRL">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/eventdetTIL">

                <EditText
                    android:layout_width="match_parent"
                    android:hint="@string/type_details_of_event"
                    android:id="@+id/eventdetailET"
                    android:layout_height="300dp" />

            </android.support.design.widget.TextInputLayout>

            <Button
                android:text="@string/submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/submitButton"
                android:layout_weight="1"
                android:layout_below="@+id/eventdetTIL"
                android:gravity="center_horizontal"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
    </RelativeLayout>

</FrameLayout>

If I write ft.replace(R.id.eventFL,fragment); in MainActivityFragment ft.replace(R.id.eventFL,fragment); then the application crashes without any logs.

If I write ft.replace(R.id.calendarFL,fragment); overlap occurs. Can you please tell me what am I doing wrong?

Here's a mistake caught in the logs. I don’t understand how the view is not located …

E / FragmentManager: No view found for id 0x7f0d0083 (com.carrickane.miftestapp: id / eventFL) for fragment EventsFragment {1752ec5 # 2 id = 0x7f0d0083}

Maybe the answer is elementary simple, but I haven't mastered it yet.

Answer:

In the replace method, the first argument is the container ID for the fragments. You are trying to fit one piece into another. This ID must be the same and must point to the markup element containing your fragments

Scroll to Top