I am very new to android development and hence, I am not able to solve my problems via help from other internet resources. Using Youtube videos, I am designing an app that uses both NavigationDrawer and TabLayout, and all the TabLayout items and navigation drawer items use fragments. My problem is that when I clicked on items in navigation drawers, the app still displays TabLayout's fragments instead of displaying the fragment corresponding to the clicked item from `NavigationDrawer. Please help me with this. I have included code and even an image below. Thanks a lot.
The abovementioned image.
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//following gives toolbar and navigation bar can be viewed by sliding L-R
Toolbar toolbar = findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
//attach sectionpageadapter to viewpager
SectionPagerAdapter pagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
ViewPager pager = findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
//attach viewpager to tablayout
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
//following to give hamburger sign to navigation drawer
drawer = findViewById(R.id.drawer_layout);
//references to listen to click events on navigation views
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//following for hamburger sign to rotate when slide from L-R
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
//below is our default fragment which will be shown on create, without any clicks
//if statement so that it wont reload on rotation or resume of the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
break;
case R.id.nav_myaccount:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AccountFragment()).commit();
break;
case R.id.nav_mymessages:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MessageFragment()).commit();
break;
case R.id.nav_mysongs:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MySongFragment()).commit();
break;
case R.id.nav_mystores:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new StoreFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
//following for closing navagation bar instead of leaving activity immediately when you press back button
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
private class SectionPagerAdapter extends FragmentPagerAdapter{
public SectionPagerAdapter(FragmentManager fm){
super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#Override
public int getCount() {
return 4;
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new HomeFragment();
case 1:
return new FragmentArtists();
case 2:
return new FragmentAlbum();
case 3:
return new FragmentPlaylists();
}
return null;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return getResources().getText(R.string.home_tab);
case 1:
return getResources().getText(R.string.artist_tab);
case 2:
return getResources().getText(R.string.album_tab);
case 3:
return getResources().getText(R.string.playlist_tab);
}
return null;
}
}
}
Replace this code!
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_home:
pager.setVisibility(View.VISIBLE);
tabs.setVisibility(View.VISIBLE);
fragmentConterner.setVisibility(View.GONE);
break;
case R.id.nav_myaccount:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AccountFragment()).commit();
break;
case R.id.nav_mymessages:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MessageFragment()).commit();
break;
case R.id.nav_mysongs:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MySongFragment()).commit();
break;
case R.id.nav_mystores:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new StoreFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
I hope it's helpful for you!
you have the provision to set the visibility of the layout elements to visible,invisible and gone. Programatically you can do this by
ViewPager.setVisibility(View.GONE);
TabLayout.setVisibility(View.GONE);
and also you can show the FrameLayout that you are using to inflate the FrameLayout container.
FrameLayout.setVisibility(View.VISIBLE);
And start with having the FrameLayout set to gone in the XML attribute.
android:visibility="gone"
I hope it helps.
Related
I add to my app Bottom Navigation Bar it's work but the problem now I want move from one button in Bottom Navigation Bar to activity that activity have to fragment to view (TabLayout,ViewPager).
When I try to make it like that selectedFragment = new MainActivityOderLIstFargmant(); the show me error like that ..
and also like that and also not work.. startActivity(new Intent(NafMain.this, MainActivityOderLIstFargmant.class));
I need to move to other activity to show two (TabLayout,ViewPager).
this activity I want to move to it ..
public class MainActivityOderLIstFargmant extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
PageAdapterOrderList pageAdapterOrderList;
TabItem tabChats;
TabItem tabCalls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mani_order_list);
tabLayout = findViewById(R.id.tablayout);
tabChats = findViewById(R.id.tabChats);
tabCalls = findViewById(R.id.tabCalls);
viewPager = findViewById(R.id.viewPager);
pageAdapterOrderList = new PageAdapterOrderList(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapterOrderList);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
}
from here
public class NafMaintest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naf_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
selectedFragment = new MainActivityOderLIstFargmant();//I try like that but not work
break;
case R.id.nav_favorites:
selectedFragment = new FragmentHome();
break;
case R.id.nav_search:
selectedFragment = new FragmentProfile();
break;
case R.id.nav_s:
selectedFragment = new MainActivity();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FragmentHome()).commit();
}
}
}
If anyone know how solution it help me
you are trying to instantiate MainActivityOderLIstFargmant activity in the switch case R.id.nav_home and put it into a Fragment variable which is called selectedFragment, so both types are different (Fragment & Activity). That is why the screenshot you provided says Incompatible types.
To get your code works you can change the switch case as below
case R.id.nav_home:
Intent intent = new Intent(NafMaintest.this, MainActivityOderLIstFargmant.class);
startActivity(intent);
return true; // use return to avoid triggering the fragment transaction after the `swtich` block
I have the following bottom navbar code to switch between 3 fragments:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
}
return loadFragment(fragment);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(new HomeFragment());
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
}
In the fragments there are RecyclerViews with lists. Every time I switch between the tabs (between fragments), it looks like the fragment is reloaded, and the lists jump to the top. I want to prevent that reloading so that the user stays on the same place in the list he viewed before switching fragments
The problem is that you are creating a new instance every time. You can cache the instance like:
private Fragment mHomeFragment = new HomeFragment();
private Fragment mDashboardFragment = new DashboardFragment();
private Fragment mNotificationsFragment = new NotificationsFragment();
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = mHomeFragment;
break;
case R.id.navigation_dashboard:
fragment = mDashboardFragment;
break;
case R.id.navigation_notifications:
fragment = mNotificationsFragment;
break;
}
return loadFragment(fragment);
}
As we could see, you are always replace your fragment when clicks on bottom navigation, replace means previous fragment removes and state cleans. The solution is do not create your fragment each time and use attach/detach method for showing actual fragment. Here is already described about these methods.
I have a navigation drawer but the main activity has nothing, only the main page fragment has some thing, I want this fragment to be called every time the application opens. Anyone have any idea how to do this?
here is an example
As in code below, add this line in your code onNavigationItemSelected(nvDrawer.getMenu().getItem(0));
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...From section above...
// Find our drawer view
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
selectDrawerItem(nvDrawer.getMenu().getItem(0));
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = FirstFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SecondFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = ThirdFragment.class;
break;
default:
fragmentClass = FirstFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
}
Actually, I'm working one app that app has side menu drawer and in a home page, tab view is there.
In that menu have 5 fragments let's say
HomeFragment
PhotosFragment
NotificationsFragment
These are in menu and in home fragment, two tabs are like recent tab and all tab
so now I'm going to say what happened, menu control and tab control working without bug but whenever app starting time only.
I mean see now app started by default it shows home fragment, in that home fragment I said know 2 tabs are there.So those are working but now coming to point what was the bug.
If I select another menu item to let's say Photos Fragment then after again I select Home Fragment those tabs are not showing.
I mean whenever app starts the first time its showing tabs after that not showing.
Why its coming like this really I don't know I checked lot ways.
this is my code
main_activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mHandler = new Handler();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
fab = (FloatingActionButton) findViewById(R.id.fab);
// Navigation view header
navHeader = navigationView.getHeaderView(0);
txtName = (TextView) navHeader.findViewById(R.id.name);
txtWebsite = (TextView) navHeader.findViewById(R.id.website);
imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg);
imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile);
// load toolbar titles from string resources
activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// load nav menu header data
loadNavHeader();
// initializing navigation menu
setUpNavigationView();
if (savedInstanceState == null) {
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
loadHomeFragment();
}
}
/***
* Load navigation menu header information
* like background image, profile image
* name, website, notifications action view (dot)
*/
private void loadNavHeader() {
// name, website
txtName.setText("Dummy");
txtWebsite.setText("www.dumnypaul.blogspot.in");
// loading header background image
Glide.with(this).load(urlNavHeaderBg)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgNavHeaderBg);
// Loading profile image
Glide.with(this).load(urlProfileImg)
.crossFade()
.thumbnail(0.5f)
.bitmapTransform(new CircleTransform(this))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgProfile);
// showing dot next to notifications label
navigationView.getMenu().getItem(3).setActionView(R.layout.menu_dot);
}
/***
* Returns respected fragment that user
* selected from navigation menu
*/
private void loadHomeFragment() {
// selecting appropriate nav menu item
selectNavMenu();
// set toolbar title
setToolbarTitle();
// if user select the current navigation menu again, don't do anything
// just close the navigation drawer
if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
drawer.closeDrawers();
// show or hide the fab button
toggleFab();
return;
}
// Sometimes, when fragment has huge data, screen seems hanging
// when switching between navigation menus
// So using runnable, the fragment is loaded with cross fade effect
// This effect can be seen in Gmail app
Runnable mPendingRunnable = new Runnable() {
#Override
public void run() {
// update the main content by replacing fragments
Fragment fragment = getHomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
fragmentTransaction.commitAllowingStateLoss();
}
};
// If mPendingRunnable is not null, then add to the message queue
if (mPendingRunnable != null) {
mHandler.post(mPendingRunnable);
}
// show or hide the fab button
toggleFab();
//Closing drawer on item click
drawer.closeDrawers();
// refresh toolbar menu
invalidateOptionsMenu();
}
private Fragment getHomeFragment() {
switch (navItemIndex) {
case 0:
// home
HomeFragment homeFragment = new HomeFragment();
return homeFragment;
case 1:
// photos
PhotosFragment photosFragment = new PhotosFragment();
return photosFragment;
case 2:
// movies fragment
MoviesFragment moviesFragment = new MoviesFragment();
return moviesFragment;
case 3:
// notifications fragment
NotificationsFragment notificationsFragment = new NotificationsFragment();
return notificationsFragment;
case 4:
// settings fragment
SettingsFragment settingsFragment = new SettingsFragment();
return settingsFragment;
default:
return new HomeFragment();
}
}
private void setToolbarTitle() {
getSupportActionBar().setTitle(activityTitles[navItemIndex]);
}
private void selectNavMenu() {
navigationView.getMenu().getItem(navItemIndex).setChecked(true);
}
private void setUpNavigationView() {
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.home:
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
break;
case R.id.nav_photos:
navItemIndex = 1;
CURRENT_TAG = TAG_PHOTOS;
break;
case R.id.nav_movies:
navItemIndex = 2;
CURRENT_TAG = TAG_MOVIES;
break;
case R.id.nav_notifications:
navItemIndex = 3;
CURRENT_TAG = TAG_NOTIFICATIONS;
break;
case R.id.nav_settings:
navItemIndex = 4;
CURRENT_TAG = TAG_SETTINGS;
break;
case R.id.nav_about_us:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
drawer.closeDrawers();
return true;
case R.id.nav_privacy_policy:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, PrivacyPolicyActivity.class));
drawer.closeDrawers();
return true;
default:
navItemIndex = 0;
}
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
}
menuItem.setChecked(true);
loadHomeFragment();
return true;
}
});
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we don't want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we don't want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.addDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawers();
return;
}
// This code loads home fragment when back key is pressed
// when user is in other fragment than home
if (shouldLoadHomeFragOnBackPress) {
// checking if user is on other navigation menu
// rather than home
if (navItemIndex != 0) {
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
loadHomeFragment();
return;
}
}
super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// show menu only when home fragment is selected
if (navItemIndex == 0) {
getMenuInflater().inflate(R.menu.main, menu);
}
// when fragment is notifications, load the menu created for notifications
if (navItemIndex == 3) {
getMenuInflater().inflate(R.menu.notifications, menu);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//no inspection SimplifiableIfStatement
if (id == R.id.action_logout) {
Toast.makeText(getApplicationContext(), "Logout user!", Toast.LENGTH_LONG).show();
return true;
}
// user is in notifications fragment
// and selected 'Mark all as Read'
if (id == R.id.action_mark_all_read) {
Toast.makeText(getApplicationContext(), "All notifications marked as read!", Toast.LENGTH_LONG).show();
}
// user is in notifications fragment
// and selected 'Clear All'
if (id == R.id.action_clear_notifications) {
Toast.makeText(getApplicationContext(), "Clear all notifications!", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
// show or hide the fab
private void toggleFab() {
if (navItemIndex == 0)
fab.show();
else
fab.hide();
}
}
My HomeFragment Code
public class HomeFragment extends Fragment {
View view;
private TabLayout tabLayout;
private ViewPager viewPager;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_home, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new MoviesFragment(), "ONE");
adapter.addFragment(new PhotosFragment(), "TWO");
adapter.addFragment(new SettingsFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
}
My Home Fragment XML Code
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
Can anybody solve me this bug
Thanks in advance
i want to replace one menu with another, in this view i have items home,products and so on.. , while clicking on products it opens another menu containing diff products.In the below code i am trying to replace another menu,but i m getting null pointer exception, but i want know that on clicking navigation item how i can switch to another navigation view?
Can anyone suggest what to do in this situation?I have attached logcat snapshot.
public void selectDrawerItem(MenuItem menuItem)
{
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId())
{
case R.id.menu_home:
fragmentClass = A.class;
break;
case R.id.menu_products:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.menu_products);
break;
case R.id.menu_B:
fragmentClass = B.class;
break;
case R.id.menu_C:
fragmentClass = C.class;
break;
case R.id.menu_D:
fragmentClass = d.class;
break;
case R.id.menu_E:
fragmentClass = E.class;
break;
case R.id.menu_F:
fragmentClass = F.class;
break;
default:
fragmentClass = A.class;
}
try
{
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
//this is line 155:
fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
drawer.closeDrawers();
}
private void setupDrawerContent(NavigationView navigationView){
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
selectDrawerItem(menuItem);
return true;
}
});
}