Android Studio Onclick Open Navigation Item - java

I'm new in Android Studio. I'm just confused with linking a navigation drawer with a button.
I have this onclicklistener:
public void clickFunc(View view) {
//codehere
i.add(new NavItem("Tip Us", R.drawable.ic_details, NavItem.ITEM, WebviewFragment.class, "http://www.google.com"));
}
When I click in a button I want to go to the navigation drawer.
I've just downloaded a project that fits my need but it uses navigation drawer and I want to use the button as a menu (not as a navigation drawer)
Anyone knows how to do it?

Related

I can't Connect navigation drawer items with there related activities and fregmant

I have made a navigation drawer in my app , but I have a problem that I can't open any activity related to any menu item in navigation drawer .
for example in this code I am trying to only display a toast message ,also there is no response when I click on the item from the menu.
I assume you are using menu items and I would suggest giving the same id for the navigation destination and its corresponding item.
if items id is nav_home then destionation id should also be nav_home in navigation.xml
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home, {*other navigations*}), binding.drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.navView.setupWithNavController(navController)
when you set it like this it will automatically connect the destination to items and u dont have to set the onclick manually

Android Studio getSupportFragment.beginTransaction().replace causing crashes when navigating to another fragment(Java)

I am trying to implement a side-navigation bar (hamburger menu). I want to be able to select a new fragment to view from this menu, and then click buttons on that fragment to direct to another fragment (not in the hamburger menu list).
I was following a guide (https://www.youtube.com/watch?v=bjYstsO1PgI), but it doesn't show the second step of redirecting to the other fragment.
I keep getting a crash whenever I click a button that redirects to the other fragment, if it has been loaded by the hamburger menu.
The line I use is to render from the hamburger menu is:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FirstFragment()).commit();
This is called by the following event listener:
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
and I get this error whenever I press a button to go to the 'other' fragment.
Navigation action/destination com.example.testApp:id/action_FirstFragment_to_submitScreen cannot be found from the current destination Destination(com.example.testApp:id/submitScreen) label=fragment_submit_screen class=com.example.renamedTestApp.SubmitScreen
This error is not present when I use
<include layout="#layout/content_main"/>
to load the first fragment on app startup (the one from the hamburger menu).
Any tips? This question may be confusing so let me know if I should tidy up my explanation.
I believe the error may be in the fragment not being fully loaded perhaps? If that is the case, what would be a better line to use to switch on the fly.
provide some code
try this
FragmentManager fragmnt= getActivity().getSupportFragmentManager();
FragmentTransaction tr= fragmnt.beginTransaction();
tr.replace(R.id.YOUR,new Yourfragment());
//or tr.replace(R.id.content_frame, new Yourfragment());
tr.commit();

Hamburger to arrow animation not working programmatically

I have made a server directory browsing app which would change contents within the Activity itself. I have been adding a feature: Navigation Drawer and handling the Hamburger and Back icon on the Toolbar as follows:
Home directory:
Hamburger icon as the default state.
Would slide the navigation drawer on clicking the hamburger or at a sliding gesture.
No state-change or animation of the hamburger when the drawer is slided.
Animation of Hamburger to Back icon when a directory is chosen.
Any child directory:
Back button from the previous animation whose sole purpose is to go to a parent directory.
Would slide the navigation drawer at a sliding gesture.
No state-change or animation of the Back icon when the drawer is slided using gesture or when it goes into an another child directory of this directory.
Animation of Back Arrow to Hamburger icon when it comes back to Home directory using back icon or onBackPressed.
I am able to get the animation of Hamburger to Back icon using this answer (Code is used verbatim is as below) but wasn't able to get the Hamburger icon again when coming back into the home directory (didn't included that code and went for another approach which is the next part):
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
// You can change this duration to more closely match that of the default animation.
anim.setDuration(500);
anim.start();
For appropriate switching between Hamburger and Back icons when browsing to and fro from home and child directories, I have used this answer (Code verbatim as below) as a reference and was able to successfully implement it for 1, 2 and 3 features of home and child directories.
private void enableViews(boolean enable) {
if(enable) {
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); // comment this line of code
if(!mToolBarNavigationListenerIsRegistered) {
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Doesn't have to be onBackPressed
onBackPressed();
}
});
mToolBarNavigationListenerIsRegistered = true;
}
}
else {
// Remove back button
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false); // comment this line of code
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
// Remove the/any drawer toggle listener
mDrawerToggle.setToolbarNavigationClickListener(null);
mToolBarNavigationListenerIsRegistered = false;
}
}
Coming to the issue at hand which is: While browsing directories, of the switching to and fro from Hamburger to Back icon, the animation part is not working at all. But the states of both the icons are successfully changed along with their functionalities. Let me know if you need some more information for troubleshooting.
You can see a working example of navigation drawer activity if you just create a new project, add an activity and use template NavigationDrawer (if you use Android Studio. Otherwise download this repo)
When I want to learn a new layout I just load up the template and then change individual pieces of code until I have what I wanted. This way you can see what does what, what stops working when you delete some line and how things should be done.
I was finally able to solve it, came to know about the behaviour of the ActionBarDrawerToggle in a much deeper way after tinkering it with the default NavigationBarActivity from Android studio.
The overriding of onDrawerSlide of the mDrawerToggle to block the animation of hamburger by the sliding drawer was the cause of the same blocking of animation of hamburger to arrow in the animatior function in the first place. Notice these two lines from the two different pieces of code (didn't include it earlier, but you get the idea):
#Override
public void onDrawerSlide(View view, float slideOffset) {
// blocks the animation
super.onDrawerSlide(view, 0);
}
// from the animator function above
mDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
Solution: I removed the overrided onDrawerSlide function, but then, the sliding drawer hamburger to arrow animation would come back as well.
Counter-Solution: I also found out the sliding drawer animation of hamburger to arrow happens due to this line: mDrawerLayout.setDrawerListener(mDrawerToggle) which again is a deprecated function. So I just commented out this line and everything is working as expected.

Why my back button does nothing?

I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack

Disable Menu Button Bringing Up Status Bar On Samsung Phones

I am running an app in full screen immersive mode.
On Samsung phones there is a menu button on the device that, when clicked, forces open the status bar at the top. This slides my entire view down and doesn't go away.
How can I prevent the menu click event from displaying the status bar?
Found it, put this in the activity.
#Override
public boolean onPrepareOptionsMenu (Menu menu)
{
return false;
}

Categories

Resources