8

I have a fragment where I try to change the title of the actionbar. This worked until i added setHasOptionsMenu(true) to the fragment. How can i solve this? Because I need the optionsmenu in the fragment.

In fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ((MainActivity)getActivity()).setActionBarTitle(getString(R.string.title_view_tree));
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.fragment_view_tree, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_view_tree,menu);
    super.onCreateOptionsMenu(menu,inflater);

}

In MainActivity:

public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

When I disable setHasOptionsMenu(true) by using // then the title of the actionbar changes correctly.

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
JDN96
  • 117
  • 1
  • 5
  • 1
    Possible duplicate of [how to change the Title in Navigation Drawer](http://stackoverflow.com/questions/33778448/how-to-change-the-title-in-navigation-drawer) – Viktor Yakunin Apr 06 '16 at 19:55
  • 1
    Did you tried to setHasOptionsMenu before the title in the action bar ? – fandro Apr 06 '16 at 20:07
  • @B378 check my answer, I've tested it, It works as you expect. Let me know if you face any more problem. – Man Jul 29 '18 at 06:24

9 Answers9

7

call the setActionBarTitle from Fragment's onCreateOptionMenu.

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main4, menu);

((MainActivity)getActivity()).setActionBarTitle(getString(R.string.title_view_tree));
        super.onCreateOptionsMenu(menu, inflater);
    }

I'd recommend to use callback interface for interacting with activity.

Man
  • 2,720
  • 2
  • 13
  • 21
1

Do the following changes to handle both Title and Menu in Fragment

public class YourFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_xml_contents, container, false);
    ((HostedActivity) getActivity()).setFragmentTitle(getActivity().getString(R.string.frag_title));
    setHasOptionsMenu(true);
    .....

    return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.profile_menu_xml, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.action_edit:
                //....... action for menu item

                return false;

            default:
                break;
        }

        return false;
    }


}
Sreehari
  • 5,621
  • 2
  • 25
  • 59
0

In the fragment that you want to set the title for: Just call

getActivity().setTitle("Fragment X");

@Override
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getActivity().setTitle("Fragment X");
}
André Abboud
  • 1,864
  • 2
  • 14
  • 23
0

Set setHasOptionsMenu in onCreate method of fragment instead of onCreateView and set title in onCreateView method:-

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

        setHasOptionsMenu(true);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ((MainActivity)getActivity()).setActionBarTitle(getString(R.string.title_view_tree));

    return inflater.inflate(R.layout.fragment_view_tree, container, false);
}
Nainal
  • 1,728
  • 14
  • 27
0

Instead of setting title from activity, you can directly set it from a fragment.

Refer below code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    getActivity().setTitle("Fragment name");
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.fragment_fragment_example, container, false);
}
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

change the Action bar title add the following code in the onCreate() of your Activity

getActionBar().setTitle("title");

0

There is an Activity called MainActivity which contain three fragment like.

Now, MainActivity has

Fragment One
Fragment Two
Fragment Three

Let say, on changing any fragment we what to change the action bar title.

This can be done by implementing FragmentManager.OnBackStackChangedListener in the activity containing those fragments.

On overriding the onBackStackChanged method, we can easily change the title and other things as per our need.

@Override
    public void onBackStackChanged() {
        try {
            for (Fragment fragment : getSupportFragmentManager().getFragments()) {
                if (fragment != null && fragment.isVisible()) {
                    if (fragment.getTag().equals(AddNewProductSelectAttributeFragment.class.getSimpleName())) {
                        setTitle(getString(R.string.select_attribute_and_product_type));
                    } else if (fragment.getTag().equals(AddSimpleProductFragment.class.getSimpleName())) {
                        setTitle(getString(R.string.add_simple_product));
                    } else if (fragment.getTag().equals(AddNewProductCategoryFragment.class.getSimpleName())) {
                        setTitle(getString(R.string.select_categories));
                    } else if (fragment.getTag().equals(AddCustomOptionFragment.class.getSimpleName())) {
                        setTitle(getString(R.string.add_custom_options));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Viral Patel
  • 1,296
  • 1
  • 11
  • 24
0

the solution is easy, just go to Res - navigation - mobile_navigation... inside you can find the line: label of your fragment, and set the tittle what you want.

0

I had set the label of the fragment in the XML file like this: android:label="Title" and it makes the get Activity.setTitle("Title") code not work, so when I removed that line it would change the title with the java code.

Olivia
  • 1
  • 4