3

When I created a new project I used Navigation Drawer Activity app screen

Now each menu item when I click on it will open a fragment by calling a method name replacement.

    public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();

            if (id == R.id.nav_home) {
                replaceFragment(0);
                setTit = "Your State Info.";
                setTitle(setTit);
 } else if (id == R.id.nav_flashcards) {

            replaceFragment(1);
            setTit = "Flash Cards";
            setTitle(setTit);

in fragment 1 I have a RadioGroup when the checked change will open the fragmet depends on the radio checked.

 @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {

        RadioButton radioButton = (RadioButton)getActivity().findViewById(i);

            if(radioButton.getTag()==1)
        ((MainActivity) getActivity()).replaceFragment(0);
           else if ((radioButton.getTag()==2))
             ((MainActivity) getActivity()).replaceFragment(2);
    }

The App work fine , but the issue is how can I change the Navigation Item Selected and also change the title for the action bar.

it's possible to use this way

 ((MainActivity) getActivity()).onNavigationItemSelected(menuitem);

but from the fragment how can I access the the items in the menu>activity_main_drawer.xml and pass it through menuitem

Mi_Dhah
  • 519
  • 1
  • 7
  • 18

4 Answers4

5

This works for me..

NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
        navigationView.getMenu().getItem(2).setChecked(true); 
Mi_Dhah
  • 519
  • 1
  • 7
  • 18
0

In your fragment you have to add setOptionsMenu(true)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • The fragment in nav_flashcards will open a new fragment that representsl nav_home , the issue is how to move the selection item from nav_flashcards to nav_home – Mi_Dhah Dec 03 '16 at 06:28
0

question is not clear. whole point of navigation drawer with fragments is to access navigation and its main toolbar all over the fragments with a single main activity.(the blue color one in your image, you should be able to change this anytime when you load a fragment)

But you need to make the frame/parent view of the fragments blow the tool bar view(that's why normally it comes with two XMLs, activtyMain.xml and contentMain.xml where content main is included below the tool bar in activityMain.XML) so you create fragments view match_parent to this contentMain.xml ,you can see toolbar is accessible to the each and every fragment you add in that view because its in the main activity where fragment/s add.

so you have a main activity and inside that you have fragment/s frame. Doesn't matter what fragment you load you can still have the access to main toolbar.

so once you check which item got clicked in navigationDrawer you load the fragment related to that right?. and there access the main toolbar of the mainActivity and do the changes that you need to display.That's it!

 if (id == R.id.nav_whatever) {
 // access toolBar or any view in main activity and do the changes 
// call relevant  fragment  
 }

But you cannot access that toolbar or mainActivity views inside your Fragment class that you try to attempt.

Edit :

Keep a Boolean in a Constant class or something similar, then when you click on your button on second fragment change the value . Now let's say Boolean is true Then you click the navigation drawer again

    // keep the id as it is and use the Boolean to check where you needs to go
     if (id == R.id.nav_whatever) {
       if(boolianName){
         // boolianName is true go load second fragment
           }else{
             // load firstfragment
            }

         }
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • What I mean is in nav_flashcards fragment there is a radio button when I click on it will open a new fragment and will replace the current one with new one and that will be the nave_home through the code ((MainActivity) getActivity()).onNavigationItemSelected , but when I open the menu the item selection still in the nav_flashcards , therefore I need a way to change the item selection through nav_flashcards to nav_home – Mi_Dhah Dec 03 '16 at 14:23
  • you mean you have a name in your navigation drawer item which has the name of 1st fragment. and you want to change it when you load the second fragment ? – Charuක Dec 03 '16 at 14:27
  • The menu item (R.id.nav_home) when click this menu open fragment 0 , menu item id (R.id.nav_flahcard) when click this menu item open fragment 1 , now inside fragment 1 there is radio button when I click this radio will open fragment 0 but if we open the menu item the selection on (R.id.nav_flahcard) therefore I need also to change the menu item selection to (R.id.nav_home) when I open the fragment 0 through the radio button – Mi_Dhah Dec 03 '16 at 14:47
0

If you use Navegation drawer, you can use this code, put it on onCreate method:

NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
        navigationView.getMenu().getItem(2).setChecked(true); 
Kevin Mendez
  • 651
  • 5
  • 10