Android - Button won't switch fragments

I've been trying to get my floating action button to work in my fragment to where it switches and does a slide animation to the next fragment but for some reason when I click on the floating action button, it does nothing. Can someone help me point out why.

Here is my fragment class

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScoutFragment extends Fragment {

    FloatingActionButton addDataScout;

    public ScoutFragment() {
        // Required empty public constructor
    } //End of ScoutFragment

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

        //Setup Floating Action Button
        FloatingActionButton addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
        addDataScout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AddScoutDataFragment fragment = new AddScoutDataFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            } //End of onClick
        }); //End of setOnClickListener

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_scout, container, false);
    } //End of onCreateView
} //End of class

Here is the xml file

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment">

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="61dp"
            android:layout_height="57dp"
            android:src="@mipmap/ic_action_add"
            app:backgroundTint="#F28016"
            app:elevation="5dp"
            app:borderWidth="0dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="12dp"
            android:layout_marginRight="10dp"
            android:scaleType="fitCenter"
            android:clickable="true">
        </android.support.design.widget.FloatingActionButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Western University"
            android:id="@+id/testText"
            android:layout_centerInParent="true"
            android:textSize="24dp"
            android:textStyle="bold" />
    </RelativeLayout>
</FrameLayout>

I am also using NavigationDrawer for my project

Answers


You are giving the container name as fragment_container but where is it? What you have to do is to give id to the FrameLayout as fragment_container.

add android:id="@+id/fragment_container" to the FrameLayout and you are ready to go.


Yes! It does nothing because:

  • R.id.data_add cannot be found in fragment_scout

The replace method of fragment transaction will replace any view in the container (R.id.data_add), however the FragmentTransaction does not manage the container.

Fix:

  • Move the replace action to fragment's parent view like this suggestion

Need Your Help

Convert a Long to base 36 in scala

scala long-integer base36

How can I convert a Long to base36 ? Along with your answer can explain how you came to the answer?