JDN96
JDN96

Reputation: 127

setTitle doesn't work in fragment

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.

Upvotes: 9

Views: 4085

Answers (9)

Olivia
Olivia

Reputation: 1

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.

Upvotes: 0

Isaias M. Poncio
Isaias M. Poncio

Reputation: 1

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.

Upvotes: 0

Viral Patel
Viral Patel

Reputation: 1316

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();
        }
    }

Upvotes: 0

Sreehari
Sreehari

Reputation: 5655

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;
    }


}

Upvotes: 1

Nithin Paul
Nithin Paul

Reputation: 76

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

getActionBar().setTitle("title");

Upvotes: 0

Jitesh Mohite
Jitesh Mohite

Reputation: 34180

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);
}

Upvotes: 0

Nainal
Nainal

Reputation: 1748

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);
}

Upvotes: 0

Man
Man

Reputation: 2817

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.

Upvotes: 6

André Abboud
André Abboud

Reputation: 2030

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");
}

Upvotes: 0

Related Questions