Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm making an app and after I implemented fragments my floating button suddenly don't respond to anything. I have tried using all the guides again but that didn't help.

I know it is alot of code, but i really dont have any idea whats wrong with it.
my code:

HTML
<pre><?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:tools="http://schemas.android.com/tools"
    tools:openDrawer="start">



    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#00FFFFFF"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:elevation="4dp"/>
    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">


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


            <RelativeLayout
                android:id="@+id/upper_section"
                android:layout_width="match_parent"
                android:layout_height="225dp"
                android:background="@drawable/bg">





                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/textView2"
                    android:layout_alignEnd="@+id/textView2"
                    android:text="0/25"
                    android:textColor="#ffffff"
                    android:textSize="30dp" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginBottom="33dp"
                    android:layout_marginEnd="26dp"
                    android:text="Gennemført"
                    android:textColor="@color/FontColor" />


            </RelativeLayout>


            <RelativeLayout
                android:id="@+id/test_id"
                android:layout_width="match_parent"
                android:layout_height="225dp"
                android:layout_marginEnd="150dp"
                android:background="@drawable/blue_line">

                <TextView
                    android:id="@+id/navn"
                    android:layout_width="wrap_content"
                    android:layout_height="69dp"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:layout_marginStart="20dp"
                    android:text="Name"
                    android:textColor="#ffffff"
                    android:textSize="30dp" />


                <TextView
                    android:id="@+id/text_view_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignStart="@+id/navn"
                    android:layout_marginBottom="14dp"
                    android:text="Torsdag den 7. juni 2018"
                    android:textColor="@color/FontColor"
                    android:textSize="12dp" />
            </RelativeLayout>

            <ListView
                android:id="@+id/listview"
                android:layout_width="386dp"
                android:layout_height="448dp"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textView3"
                android:layout_marginStart="6dp" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/upper_section"
                android:layout_marginBottom="10dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="10dp"
                android:text="Indkøbsliste"
                android:textColor="@color/Completed"
                android:textSize="15sp" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/listview"
                android:layout_alignParentBottom="true"
                android:layout_gravity="end|bottom"
                android:layout_marginBottom="22dp"
                android:src="@drawable/ic_add"
                app:fabSize="normal" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignStart="@+id/textView3"
                android:layout_marginBottom="34dp"
                android:text="Completed"
                android:textColor="@color/Completed"
                android:textSize="15sp" />


        </RelativeLayout>
    </FrameLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>



Java
public class MainActivity extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener{


    private DrawerLayout drawer;
    TextView fadetxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawer = findViewById(R.id.drawer_layout);
        fadetxt = findViewById(R.id.textView2);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
            //presentActivity(view);

        //Drawer
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

            txtfade();



        Calendar calendar = Calendar.getInstance();
        String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());

        TextView textViewDate = findViewById(R.id.text_view_date);
        textViewDate.setText(currentDate);

        TextView nava = findViewById(R.id.navn);
        String s = "Navn"
                + System.getProperty("line.separator")
                + "Indkøbsliste"
                + System.getProperty("line.separator");


        nava.setText(s);
        txtfade();

    }





    //Text fade
    public void txtfade(){
        Animation animation = new AlphaAnimation(0.00f,1.00f);
        animation.setDuration(3000);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                fadetxt.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                fadetxt.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        fadetxt.startAnimation(animation);

    }


    // Drawer Fragment Navigation

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_family:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new MessageFragment()).commit();
                break;
            case R.id.nav_settings:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new settingsFragment()).commit();
                break;
            case R.id.nav_madplan:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new familyFragment()).commit();
                break;
        }

        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            startActivity(new Intent(this,MainActivity.class));
        }
    }


    //Circular Reveal

    public void presentActivity(View view) {
        ActivityOptionsCompat options = ActivityOptionsCompat.
                makeSceneTransitionAnimation(this, view, "transition");
        int revealX = (int) (view.getX() + view.getWidth() / 2);
        int revealY = (int) (view.getY() + view.getHeight() / 2);

        Intent intent = new Intent(this, addItem.class);
        intent.putExtra(addItem.EXTRA_CIRCULAR_REVEAL_X, revealX);
        intent.putExtra(addItem.EXTRA_CIRCULAR_REVEAL_Y, revealY);

        ActivityCompat.startActivity(this, intent, options.toBundle());
    }
}


What I have tried:

I have already been searching the web but that didnt help
Posted
Updated 14-Jun-18 4:56am
v3
Comments
David Crow 15-Jun-18 23:09pm    
Have you tried using Android Studio to create a Navigation Drawer Activity to see where it puts the FloatingActionButton in relation to the other components?

1 solution

Your DrawerLayout is intercepting all the click events. You can prevent that by adding:
android:clickable="false"
android:focusable="false"
 
Share this answer
 
Comments
Member 13864446 15-Jun-18 2:27am    
i just made the drawer_layout
android:clickable="true"
android:focusable="true"

but that didnt help?
did i do anything wrong?
[no name] 15-Jun-18 2:28am    
You set it to true, but I wrote false? :)
Member 13864446 15-Jun-18 3:08am    
i have also tried "false" as you said
Member 13864446 15-Jun-18 2:55am    
yeah my bad

my code looks like this now

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools"
tools:openDrawer="start"
android:clickable="false"
android:focusable="false">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900