Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar as action bar in Fragment

Tags:

java

android

In my app, I use NoActionBar style, and I want to add action Bar to the one of my fragment.

In Activity I can do this like:

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

How do this in fragment ?

I tried so:

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

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Toolbar actionBar = (Toolbar) getActivity().findViewById(R.id.my_app_bar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(actionBar);

        return inflater.inflate(R.layout.fragment_training, container, false);

    }

But it doesn't work.

UPD I think the problem in this line:

Toolbar actionBar = (Toolbar) getActivity().findViewById(R.id.my_app_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(actionBar);
like image 520
Alexandr Svetenco Avatar asked Sep 15 '16 14:09

Alexandr Svetenco


People also ask

How do I access the activity toolbar in fragment?

Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen. To use the Toolbar in your fragment, provide an ID and obtain a reference to it in your fragment, as you would with any other view.

How do we hide the menu on toolbar in one fragment?

How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.

How do you add action items to the action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What is Androidx Appcompat widget toolbar?

androidx.appcompat.widget.Toolbar. A standard toolbar for use within application content. A Toolbar is a generalization of action bars for use within application layouts.


1 Answers

Old question)), should be:

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

     //Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.my_app_bar);
     //((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
     //return inflater.inflate(R.layout.fragment_training, container, false);

     View v = inflater.inflate(r.layout.fragment_training, container, false);
     Toolbar toolbar = (Toolbar) v.findviewbyid(r.id.my_app_bar);
     ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
     return v;
}
like image 135
Alexandr Svetenco Avatar answered Sep 23 '22 17:09

Alexandr Svetenco