Android setting fragment toolbar title - java

I was having some minor bug with fragment transaction. The problem occurs when I opened up more than one fragment and press the back button and the title shown on toolbar is wrong.
For instance, from my Home -> Expenses fragment, and I press back button from Expenses fragment, the title shown on toolbar is correct.
However, let's say Home -> Expenses -> Incomes fragment, and I press back button from Incomes fragment, it will go back to Home fragment, but the title shown on toolbar will be 'Expenses' instead of 'Home'.
Here is my set up for MainActivity which extends AppCompatActivity:
// inside onCreate() put all these code
//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) {
//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);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
final Bundle bundle = new Bundle();
switch (menuItem.getItemId()){
case R.id.home:
getSupportActionBar().setTitle("Home");
return true;
case R.id.expenses:
return true;
case R.id.incomes:
return true;
case R.id.history:
return true;
case R.id.setting:
return true;
}
}
});
Then inside each of my fragments which extends Fragment, I set the title and override the back button:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_expense,container,false);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Expenses");
// override on back key pressed back to main fragment
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK ) {
getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home");
return true;
} else {
return false;
}
}
});
return v;
}
I even added this line ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home"); to set the title when back button is on click but it is futile.

Try it this way: 1. In your activity override onBackPress:
#Override public void onBackPressed() {
if (count == 0) {
super.onBackPressed();
}
else {
getSupportFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
Remove the KeyListener of the fragment.

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("fragment title");
Just put this line in onCreateView() fragment.
and don't override back button in fragment override it in activity.

Related

Navigation component with bottom navigation in android [duplicate]

I have an Android app (Java) which uses the Navigation Component to set up a Bottom Navigation. The app consists of a single Activity (Main), everything else is loaded in fragments.
The idea is for the Splash Screen to launch and check if the user is logged in or not. If the user is logged in, then the home screen loads and the bottom navigation (previously hidden) becomes visible.
The bottom navigation consists of 3 tabs.
However, when the user signs in, it can be one of two types of users. If he's a subscriber he will get access to all sections of the app. However if the user is a visitor, he will get access to only two sections. In this case I want to have a different bottom navigation menu to be visible with only 2 tabs.
How can I replace the bottom navigation depending on the user type if the main bottom navigation has already been assigned in the main activity?
This is the code in the MainActivity:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FragmentManager supportFragmentManager = getSupportFragmentManager();
NavHostFragment navHostFragment = (NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.homeFragment, R.id.libraryFragment, R.id.searchFragment, R.id.myStuffFragment).build();
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
NavigationUI.setupWithNavController(bottomNav, navController);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
#Override
public void onDestinationChanged(#NonNull NavController controller, #NonNull NavDestination destination, #Nullable Bundle arguments) {
if (destination.getId() == R.id.splashScreenFragment || destination.getId() == R.id.welcomeFragment || destination.getId() == R.id.signInFragment) {
toolbar.setVisibility(View.GONE);
bottomNav.setVisibility(View.GONE);
} else if (destination.getId() == R.id.audioPlayerFragment) {
bottomNav.setVisibility(View.GONE);
} else {
toolbar.setVisibility(View.VISIBLE);
bottomNav.setVisibility(View.VISIBLE);
}
}
});
This is the code in the SplashScreenFragment (this is the startDestination in the nav_graph):
public class SplashScreenFragment extends Fragment {
private static final String TAG = "SplashScreenFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_splash_screen, container, false);
return v;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
SharedPreferences getSharedData = this.getActivity().getSharedPreferences("AppTitle", Context.MODE_PRIVATE);
Boolean loggedIn = getSharedData.getBoolean("isUserLoggedIn", false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (!loggedIn) {
Navigation.findNavController(view).navigate(R.id.action_splashScreenFragment_to_welcomeFragment);
} else {
Navigation.findNavController(view).navigate(R.id.action_splashScreenFragment_to_homeFragment);
}
}
}, 2000);
}
You can have the full menu items by default in the BottomNavigationView, so when a subscribed user logged in, then no change in the menu is required.
And if the user is not subscribed, then you can remove the needed items from the menu programmatically using removeItem()
So, in MainActivity add a condition :
if (notSubscribedUser) {
if (bottomNav.getMenu().findItem(R.id.my_item_id) != null)
bottomNavigationView.getMenu().removeItem(R.id.my_item_id);
}
And do the same for all menu item ids that you want to remove

Swipe between fragments with help of BottomNavigationBar

I have trouble finding a good example on how to swipe between fragments with help of bottom navigatiom bar. Since FragmentStatePagerAdapter is deprecated and a new ViewPager2 is now recommended instead of the old ViewPager I want to use ViewPager2 and FragmentStateAdapter in my code instead. I have found an example of how to combine BottomNavigationBar and ViewPager here and I want to do something similar. My code have many similarities to the one in the example with the only difference that I have my code in a fragment instead of an activity. Here is a picture of how my FrontendFragment display look like. I can switch between the views using the bottomnavigationbar but I also want to be able to swipe between the views. Can someone help me or at least direct me on the right way? Here is my code:
FragmentPagerAdapter Class:
public class FragmentPagerAdapter extends FragmentStateAdapter {
private static final int mFragmentCount = 5;
public FragmentPagerAdapter(#NonNull Fragment fragment) {
super(fragment);
}
#NonNull
#Override
public Fragment createFragment(int position) {
switch (position){
case 0:
return new HomeFragment();
case 1:
return new SearchFragment();
case 2:
return new AddFragment();
case 3:
return new MessageFragment();
case 4:
return new ProfileFragment();
}
return null;
}
#Override
public int getItemCount() {
return mFragmentCount;
}
}
FrontendFragment Class:
public class FrontendFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener{
private BottomNavigationView mBottomNavigationView;
private ViewPager2 mViewPager2;
private FragmentPagerAdapter mFragmentPagerAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_frontend, container, false);
loadFragment(new HomeFragment());
mBottomNavigationView = v.findViewById(R.id.bottomNavigationBar);
mBottomNavigationView.setOnNavigationItemSelectedListener(this);
return v;
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.home_icon:
selectedFragment = new HomeFragment();
break;
case R.id.search_icon:
selectedFragment = new SearchFragment();
break;
case R.id.add_icon:
selectedFragment = new AddFragment();
break;
case R.id.message_icon:
selectedFragment = new MessageFragment();
break;
case R.id.profile_icon:
selectedFragment = new ProfileFragment();
break;
}
return loadFragment(selectedFragment);
}
private boolean loadFragment(Fragment selectedFragment) {
if(selectedFragment != null){
MainActivity.sFm.beginTransaction().replace(R.id.relLayoutMiddle, selectedFragment).commit();
return true;
}
return false;
}
}
Thanks in advance!
As I'm already using BottomNav with ViewPager2 in one of my app, I can help.
Your code is partially correct which means your FragmentPagerAdapter is fine, but not your FrontEndFragment.
See, the FragmentPagerAdapter has to be set to a ViewPager2 as
//this here is the FrontEndFragment
mViewPager2.setAdapter(new FragmentPagerAdapter(this));
Then, You don't have to do the FragmentTransaction at all, you just have to change the ViewPager2's current item position through the BottomNavigationBar as
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home_icon:
mViewPager2.setCurrentItem(0);
break;
case R.id.search_icon:
mViewPager2.setCurrentItem(1);
break;
case R.id.add_icon:
mViewPager2.setCurrentItem(2);
break;
case R.id.message_icon:
mViewPager2.setCurrentItem(3);
break;
case R.id.profile_icon:
mViewPager2.setCurrentItem(4);
break;
}
return false;
}
This is all, you don't have to deal with Fragments at all apart from the FragmentPagerAdapter. Also, don't forget to remove the loadFragment(new HomeFragment()); which is not required, nor the function loadFragment() is required.
(Optional), Furthermore, if you want to disable the Swipe Action of the ViewPager2 and want the Fragments to be selected based on the Selected BottomNav item only, then you can set setUserInputEnabled() property of ViewPager2 as false.
Next, to set the BottomNavigationBar's item as selected based on the swipe of the ViewPager2, what you've to do is,
Create a global var
MenuItem previousMenuItem;
Then, set a default item (first) to be selected of BottomNav on activity start as
mBottomNavigationView.getMenu().getItem(0).setChecked(true);
Finally, set an OnPageSelected() callback on ViewPager2 to update the selected Menu Item as:
mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
if (previousMenuItem != null) {
previousMenuItem.setChecked(false);
}
else {
mBottomNavigationView.getMenu().getItem(0).setChecked(false);
}
mBottomNavigationView.getMenu().getItem(position).setChecked(true);
previousMenuItem = mBottomNavigationView.getMenu().getItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
What you're doing here is that you're setting default item to previousMenuItem and then on swiping to a different page, deselecting the previousMenuItem and selecting the new one, which means updating the BottomNav based on ViewPager's current item. This is the complete code you require to acheive your objective.
If you want to swipe between views, a simple solution would be to store all views in your parent view, but set all layouts for views except the initial view to android:visibility="gone". Make sure to set the initial view layout to android:visibility="visible" though. Now on you button clicks, you will have to implement onClick such that they turn on/ off view visibilities accordingly. For example, store views in order and control them via array index. But the whole thing you're trying to do is generally not a good design pattern in my opinion.
Why don't you load another Activity onClick instead of crowding your single activity? This will cause load time issues. Even if the views are non-visible, it's just an overall hassle to maintain all that in one place.

Navigation Menu Fragment not being able to initialize its layout's components

I've created a navigation menu fragment for my application so that i can use it across all of my activities. However, when i run the app the menu does not work neither when i click on the hamburger icon nor when i pull from left to right. So i decided to use the debugger and i found out that none of my fragment's layout components get initialized within the fragment's class. Can you help me figure out why this happens and how to fix it? Here is my fragment.java class and my fragment_layout.xml file:
Fragment.java:
public class NavMenuFragment extends Fragment {
// NavMenu member vars
private DrawerLayout mDrawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle mToggle; // Button for toggling the side menu
// Keeps the position of the previously selected menu item(0 : Home)
int position = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_nav_menu,null);
mDrawerLayout = view.findViewById(R.id.drawerLayout);
navigationView = view.findViewById(R.id.nav_view);
mToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,R.string.drawer_open,R.string.drawer_closed); // Instantiating our button
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Sets the default selected menu item, to the Home item
navigationView.getMenu().findItem(R.id.nav_home).setChecked(true);
// Used to help on check and uncheck menu items when the user clicks on them
final List<MenuItem> items = new ArrayList<>();
Menu menu;
menu = navigationView.getMenu();
// Fill the list with all the menu items
for(int i=0; i<menu.size();i++) {
items.add(menu.getItem(i));
}
Toast.makeText(getActivity(), "size:" + items.size(), Toast.LENGTH_SHORT).show();
// When an item inside the NavView gets clicked, then handle the event...
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
mDrawerLayout.closeDrawer(Gravity.START);
break;
case R.id.nav_UserBoxGLB:
break;
case R.id.nav_UserBoxJP:
break;
case R.id.nav_settings:
break;
case R.id.nav_feedback:
break;
case R.id.nav_contact_us:
break;
case R.id.nav_donate:
// Open the website's URL in a browser window
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
break;
case R.id.nav_about:
break;
default:
return onNavigationItemSelected(item);
}
items.get(position).setChecked(false);
item.setChecked(true);
mDrawerLayout.closeDrawers();
return false;
}
});
mDrawerLayout.addDrawerListener(mToggle);
// Set the hamburger icon's color
mToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.NavActionBarTextColor));
mToggle.syncState();
}
// When an item from the Action Bar gets tapped, then...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return mToggle.onOptionsItemSelected(item) || onOptionsItemSelected(item);
}
}
Fragment_layout.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:id="#+id/drawerLayout">
<!-- The actual side menu Nav View -->
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu"
android:id="#+id/nav_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
You are doing it wrong
If you want to use same navigation drawer for all fragment then
You can make a navigation drawer in your main activity and open fragment from that navigation drawer.
Here is the tutorial

BottomNavigation Item Selected Animation onBackPressed

I have a app with BottomNavigation (3 Items). The Item 1 load Fragment 1, Item 2 load fragment 2 and Item 3 load fragment 3. When one item is selected in the BottomNavigation, the view of this item keep blue and the text is more big. I have implemented onBackPressed, for it back the fragment history (BackStack). But, when i'm backing, the views in BottomNavigation are stoppe. So, if i'm in fragment 3, and i press back button and it back to fragment 2, the BottomNavigationView show that i'm in fragment 3, and if i press back button again, i go to fragment1, but the BottomNavigationView don't update, it keep in Fragment 3. The screenshorts will show what i'm talking about.
Sorry my bad english and my bad explanation.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = Fragment1.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).addToBackStack(null).commit();
return true;
case R.id.navigation_dashboard:
selectedFragment = Frament2.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).addToBackStack(null).commit();
return true;
case R.id.navigation_notifications:
selectedFragment = Fragment3.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).addToBackStack(null).commit();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
From the Material Design guidelines for bottom navigation:
On Android, the Back button does not navigate between bottom navigation bar views.
And from the Developer Training for Back Navigation:
Note: You should not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs)
So you should not use the back stack when replacing fragments based on user interaction with your BottomNavigationView.
If you choose to ignore these guidelines, probably the answer is to look into FragmentManager.OnBackStackChangedListener and activate the appropriate navigation item when you pop the back stack.

how to highlight the selected item in the menu on the navigation bar drawer

i already created a navigation drawer bar with a menu but i want to highlight and disable the selected item on the menu bar when i check the drawer bar.
how can i do that ?
also i did not see the click animation when i click the item on the menu
please help
Thanks.
this is the code i used
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if(child!=null && mGestureDetector.onTouchEvent(motionEvent)){
Drawer.closeDrawers();
Toast.makeText(MainActivity.this, "The Item Clicked is: " + recyclerView.getChildPosition(child), Toast.LENGTH_SHORT).show();
int pos = recyclerView.getChildPosition(child);
if(pos== 1){
Intent intent = new Intent(getApplicationContext(),About.class);
startActivity(intent);
}
return true;
}
return false;
}
#Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
RecyclerView does not handle item selection or states like a ListView does. Instead you have to handle this manually in your view holder.
The first thing you can do is declare your item view as clickable, in your `ViewHolder constructor :
public ViewHolder(View itemView) {
super(itemView);
// Make this view clickable
itemView.setClickable(true);
// ...
}
http://www.grokkingandroid.com/statelistdrawables-for-recyclerview-selection/

Categories