0

So I am trying to open a new activity from a drawable menu. I want to get from "Home" activity to "Welcome" activity. Here is what I had done :

Edit: So I changed the order from my xml file but did not solve the issue. I changed the preferences settings as presented in one of the 3 posts suggested that might be similar to my problem but that did not worked either (they were already ok anyway). I updated with more information in my Home.class. Maybe I do something wrong there?

Here is my Home activity :

     package com.example.padmw;

import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;

import com.example.padmw.Common.Common;
import com.example.padmw.Interface.ItemClickListener;
import com.example.padmw.Model.Category;
import com.example.padmw.ViewHolder.MenuViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Menu;
import android.widget.TextView;

public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private AppBarConfiguration mAppBarConfiguration;

    FirebaseDatabase database;
    DatabaseReference category;
    TextView txtFullName;
    RecyclerView recycler_menu;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Toolbar toolbar = findViewById(R.id.toolbar);

        toolbar.setTitle("Menu");
        setSupportActionBar(toolbar);

        //Firebase
        database = FirebaseDatabase.getInstance();
        category = database.getReference("Category");

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Coming soon!", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        DrawerLayout drawer = findViewById(R.id.drawer_layout);



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

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        //Name for user
        View headerView = navigationView.getHeaderView(0);
        txtFullName = (TextView) headerView.findViewById(R.id.txtFullName);
        txtFullName.setText(Common.currentUser.getName());

        //Load Menu
        recycler_menu = (RecyclerView) findViewById(R.id.recycler_menu);
        recycler_menu.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recycler_menu.setLayoutManager(layoutManager);
        loadMenu();


    }

    private void loadMenu() {
        adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class, R.layout.menu_item, MenuViewHolder.class, category) {

            @Override
            protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position) {
                viewHolder.txtNameMenu.setText(model.getName());
                Picasso.get().load(model.getImage())
                        .into(viewHolder.imageView);
                final Category clickItem = model;
                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        //Get CategoryId and sed it to NewActivity
                        Intent foodList = new Intent(Home.this, FoodList.class);
                        foodList.putExtra("CategoryId", adapter.getRef(position).getKey());
                        startActivity(foodList);
                    }
                });
            }
        };
        recycler_menu.setAdapter(adapter);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActivity(new Intent(Home.this, SettingsActivity.class));
            return true;

        }

        return super.onContextItemSelected(item);

    }


    @Override

    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            int id = menuItem.getItemId();
            if (id == R.id.nav_video) {

                startActivity(new Intent(Home.this, Welcome.class));
            }else if (id==R.id.nav_log_out){
                startActivity(new Intent(Home.this, MainActivity.class));
            }

            DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        }

}

And here is my Welcome activity :

public class Welcome extends AppCompatActivity  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        if(findViewById(R.id.welcome_container)!= null)
        {
            if(savedInstanceState !=null)
                return;

            getSupportFragmentManager()
                    .beginTransaction().add(R.id.welcome_container,new WelcomeFragment()).commit();
        }

    }



}

Here is the WelcomeFragment.java:

public class WelcomeFragment extends Fragment {


    public WelcomeFragment() {
        // Required empty public constructor
    }


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

}

My problem is that when I click on the item, nothing actually happens. What went wrong? If you want me to add the layouts, please just comment down below.

Edit: My activity_home file (res\layout\activity_home.xml) :

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/background"
    tools:openDrawer="start">


        <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/colorPrimaryDark"
        app:itemTextColor="@android:color/white"
        app:itemIconTint="@android:color/white"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/activity_home_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

And here is my activity_home_drawer file (res\menu\activity_home_drawer):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">


    <group android:checkableBehavior="single">
        <item
        android:id="@+id/nav_video"
        android:icon="@drawable/ic_ondemand_video_black_24dp"
        android:iconTint="@android:color/white"
        android:title="@string/ItemTitle_5"
        tools:targetApi="o"

            />
        <item
            android:id="@+id/nav_menu"
            android:icon="@drawable/ic_restaurant_black_24dp"
            android:iconTint="@android:color/white"
            android:title="@string/ItemTitle_1"
            tools:targetApi="o"/>

        <item
            android:id="@+id/nav_cart"
            android:icon="@drawable/ic_shopping_cart_black_24dp"
            android:iconTint="@android:color/white"
            android:title="@string/ItemTitle_2"
            tools:targetApi="o" />

        <item
            android:id="@+id/nav_order"
            android:icon="@drawable/ic_access_time_black_24dp"
            android:iconTint="@android:color/white"
            android:title="@string/ItemTitle_3"
            tools:targetApi="o"/>
        <item
            android:id="@+id/nav_log_out"
            android:icon="@drawable/ic_exit_to_app_black_24dp"
            android:iconTint="@android:color/white"
            android:title="@string/ItemTitle_4"
            tools:targetApi="o"/>
    </group>


</menu>
Chris911
  • 909
  • 1
  • 6
  • 10
  • can you show me the menu file that you added to navigation drawer – Furqan Khan Sep 04 '19 at 18:40
  • Debug the onNavigationItemSelected and check if startActivity is called – Gabriele Mariotti Sep 04 '19 at 19:39
  • @Furqan Khan You mean the xml file? – Chris911 Sep 05 '19 at 03:57
  • Please [edit] your question to add the `activity_home` layout, or whichever layout has the `` in it. – Mike M. Sep 05 '19 at 03:59
  • @Mike M. Edited – Chris911 Sep 05 '19 at 04:13
  • Oops, never mind. I missed the ``. In that case, the problem is the layout order. The drawer in a `` must be listed last in order for it to receive touch events properly. Move the `` to after the ``, inside the ``. If you did not modify that file to be like that yourself, then it's possibly due to an issue in the 3.5 upgrade for Android Studio that causes XML to be rearranged incorrectly. If that's your case, have a look at [this post](https://stackoverflow.com/q/57591080) to see how to fix that. – Mike M. Sep 05 '19 at 04:18
  • @Mike M. I applied the solutions presented on all 3 posts, but none worked. I updated my xml file in this post and also added the rest of the Home.class. Maybe I did something wrong there? – Chris911 Sep 05 '19 at 16:31
  • That is a completely different setup than what you had originally posted. You have the layout correct, now, but you've got conflicting patterns in your coding. The `NavigationUI.setupWithNavController()` call is overriding the `navigationView.setNavigationItemSelectedListener()` call. If you're actually using Navigation, then get rid of your `OnNavigationItemSelectedListener` setup, and add those Activities to your Navigation graph appropriately. If you don't mean to be using Navigation, then get rid of the `NavigationUI` stuff, and your `OnNavigationItemSelectedListener` will start working. – Mike M. Sep 05 '19 at 16:38
  • I added another duplicate to the list, one that covers this scenario, since that's apparently what you're actually running. In any case, I would mention that the Navigation drawer pattern isn't really meant to be used with separate Activities. Your drawer should really only be switching out `Fragment`s in the one `Activity`. – Mike M. Sep 05 '19 at 16:44

0 Answers0