Home java How to add items to toolbar if it is not registered in...

How to add items to toolbar if it is not registered in Activity_main.xml?

Author

Date

Category

In Android Studio, create an empty activity. How to add let’s say a button to this standard toolbar?


Answer 1, Authority 100%

If not in the markup, but on the topic there is, you need to take a system bar:

getsupportactionbar ()

Next after this is needed in the RES / MENU folder (if not Create) Add a menu, for example, Main_Menu.xml (file took, for example):

& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
  & lt; Menu
    XMLNS: Android = "http://schemas.android.com/apk/res/android"
    XMLNS: App = "http://schemas.android.com/apk/res-auto"
    XMLNS: Tools = "http://schemas.android.com/tools"
    Tools: context = ". MainActivity" & gt;
    & lt; item
      Android: id = "@ + ID / ACTION_SETTINGS"
      Android: OrderIncategory = "100"
      Android: title = "@ STRING / ACTION_SETTINGS"
      App: ShowasAction = "Never" / & gt;
    & lt; item
      Android: id = "@ + ID / Action_Search"
      Android: OrderIncategory = "200"
      Android: title = "SEARCH"
      Android: icon = "@ Drawable / Ic_Search"
      App: ShowASAction = "IFROOM" / & GT;
    & lt; item
      Android: id = "@ + ID / Action_user"
      Android: OrderIncategory = "300"
      Android: title = "User"
      Android: icon = "@ Drawable / Ic_user"
      App: ShowASAction = "IFROOM" / & GT;
  & lt; / menu & gt;

Next go into activity and attach the created menu to your toolbar and click Ctrl + o and we are looking for methods below:

@ override
Public Boolean OnCreateOptionsMenu (Menu Menu) {
getMenuinflater (). Inflate (R.Menu.Menu_Main, Menu);
RETURN TRUE;
}
@Override
Public Boolean OnoPtionSetemSelected (MenuItem Item) {
int id = item.getitemid ();
if (id == r.id.action_settings) {
  RETURN TRUE;
}
RETURN SUPER.ONOPTIONSIEMSELECTED (Item);
}

And make the choice of items that you have done in resources. Here there is Tutorial on this issue.

Update

After creating Empty Activity you will need to check there is no one in the topic of Tulbara, because after dragging them there will be two. If I understand correctly, then by dragging you imply the use of palette and widgets in it. After you dragged Tulbar in the design editor, you will have something like this:

& lt; androidx.appompat.widget.toolbar
   Android: id = "@ + id / toolbar"
   Android: minHeight = "? attr / actionbarsize"
   Android: layout_width = "Match_Parent"
   Android: layout_height = "wrap_content"
   App: TitleTextColor = "@ Android: Color / White"
   Android: background = "? ATTR / ColorPrimary" & gt;
  & lt; /androidx.appcompat.widget.toolbar>

Next you can work with it from the activity:

toolbar toolbar = (toolbar) FindViewByid (r.id.toolbar);
SetSupportactionBar (Toolbar);

Well, of course use the above methods to work with the menu from resources.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions