0

I would like to have an Activity populated with Fragments. Every Fragment behaves as a windows in Windows OS - it has its Toolbar with title and action buttons:

enter image description here

Fragment:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Show Toolbar in Fragment
        setHasOptionsMenu(true);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_entries_list, container, false);
    }

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

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

Question:

  1. How to change the title of the Fragment?
  2. How to display three-dot actions button?

Edit:

I have now modified menu_common.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_categories"
        android:title="Categories"
        android:visible="true"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_settings"
        android:title="Settings"/>
</menu>

Fragment

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup view = (ViewGroup) inflater.inflate(
                R.layout.fragment_entries_list, container, false);

        // Toolbar
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.tb_actions);
        toolbar.setTitle("Bar");
        toolbar.showOverflowMenu();
        setHasOptionsMenu(true);

        // Inflate the layout for this fragment
        return view;
    }

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

Edit 2:

This is what I want to achieve:

enter image description here

0leg
  • 13,464
  • 16
  • 70
  • 94

2 Answers2

1
  1. How to change the title of the Fragment?

Include Toolbar in your fragment and call toolbar.setTitle(String).

  1. How to display three-dot actions button?

As you can see in this answer:

toolbar.showOverflowMenu();
Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • Have modified the question - the title is set, the action button is still not showing. Wonder if one should add smth similar to `setActionBar(toolbar)`? – 0leg Mar 30 '17 at 16:04
  • Btw, using `android.support.v7.widget.Toolbar`. – 0leg Mar 30 '17 at 16:05
  • @h3d0, what problem do you have? There can be only one action bar in the activity. If that's what you need - then you can perform [`setSupportActionBar(toolbar)`](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#setSupportActionBar(android.support.v7.widget.Toolbar)) – azizbekian Mar 30 '17 at 16:06
  • I want to have Activity with ActionBar, populated with the Fragments (like a ListView). Each Fragment has **its own** ActionBar. – 0leg Mar 30 '17 at 16:08
  • You can have only one action bar in an activity. But you can have any number of `Toolbar`s you want in each `Fragment`. `Toolbar` is just a `View`. – azizbekian Mar 30 '17 at 16:09
  • Exaclty, I want many `Toolbars` in `Fragments` (1 `Toolbar` per `Fragment`). The problem is that none of those toolbars render the actions button. Everything else works fine. So, `toolbar.showOverflowMenu()` - doesn't work. – 0leg Mar 30 '17 at 16:13
  • `none of those toolbars render the actions button` you mean 3-dot icon? – azizbekian Mar 30 '17 at 16:16
  • Yes, the 3-dot icon is not getting shown. I would to fix this, so **it is** getting shown. – 0leg Mar 30 '17 at 16:18
  • Those icons will be shown if there is `Menu` attached to the toolbar. But as long as your `Toolbar`s are not `ActionBar`, so they have no `Menu`. You can declare your custom layout within `Toolbar` tag in `xml`. See [here](http://stackoverflow.com/a/32775265/1083957), just add an `ImageView` with 3-dot icon. `Toolbar` is a `ViewGroup`, you can add your views inside it as you wish. – azizbekian Mar 30 '17 at 16:21
  • Well, there has to be another way. The same tooltip/menu works fine on all Activities. This 3-dot button only not getting shown in the `Fragment`. – 0leg Mar 30 '17 at 16:35
  • The purpose of the button is to show that there are some menu items in there. Unless you have them, I cannot see how `Toolbar` will show that icon. – azizbekian Mar 30 '17 at 16:41
  • That makes sense. In this case, question is: how does one populate Toolbar with the items in Fragment? – 0leg Mar 30 '17 at 17:02
  • Overriding `onCreateOptionsMenu()`. – azizbekian Mar 30 '17 at 17:05
  • I have figured out that `onCreateOptionsMenu` is never called. Tried to move `setHasOptionsMenu(true)` to `onAttach`, `onResume` - still nothing. – 0leg Mar 30 '17 at 17:53
0

It seems not possible to have more then 1 Toolbar on the page (at least I haven't found the way).

My solution was to use custom TextView and ImageView (icon-button) along with PopupMenu for each Fragment.

0leg
  • 13,464
  • 16
  • 70
  • 94