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>