Good day. It is required to make a menu in the toolbar :

toolbar

So far it turns out not quite that)

toolbar_2

In onPrepareOptionsMenu() I try to set the text, test the position of the textView , catch the NPE. Please tell me how to implement our plans and remove the name of the application Riddle , maybe I started wrong, what do I do through menu.xml ? Also in the menu.xml there is no alignment for the elements, they go one by one, and here it is needed. I tried to shove elements right away in toolbar.xml , but there they are very inconspicuous and stretched, menu.xml in menu.xml , everything menu.xml out beautiful, but "unmanaged".

MainActivity

 public class MainActivity extends AppCompatActivity { Toolbar toolbar; public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public boolean onPrepareOptionsMenu(Menu menu) { TextView item = (TextView) menu.findItem(R.id.textview_balance); item.setText("test"); return super.onPrepareOptionsMenu(menu); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } } 

menu / main.xml

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/button_back" android:title="@string/button_back" android:icon="@drawable/button_back" yourapp:showAsAction="always" /> <TextView android:id="@+id/textview_balance" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <item android:id="@+id/button_hint" android:title="@string/button_hint" android:icon="@drawable/button_hint" yourapp:showAsAction="always" /> <item android:id="@+id/button_nextlevel" android:title="@string/button_nextlevel" android:icon="@drawable/button_nextlevel" yourapp:showAsAction="always" /> </menu> 

activity_main.xml

 <include layout="@layout/toolbar" android:id="@+id/toolbar" /> 

toolbar.xml

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> </android.support.v7.widget.Toolbar> 

    2 answers 2

    I suggest not to use the menu, but to set the location of the ImageButton s in the Toolbar layout:

     <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" android:background="@color/colorPrimary" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageButton android:id="@+id/left_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:background="@color/colorPrimary" android:src="@drawable/arrow_left" android:onClick="onClick"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" /> <ImageButton android:id="@+id/center_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:background="@color/colorPrimary" android:src="@drawable/dollars155" android:onClick="onClick"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" /> <ImageButton android:id="@+id/right_left_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:background="@color/colorPrimary" android:src="@drawable/question_sign" android:onClick="onClick"/> <ImageButton android:id="@+id/right_right_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:background="@color/colorPrimary" android:src="@drawable/arrow_right_in_ring" android:onClick="onClick"/> </LinearLayout> </android.support.v7.widget.Toolbar> 

    Next in the Activity implement the onClick() method:

     public void onClick(View view) { int id = view.getId(); switch (id) { case R.id.left_button: // делайте то, что нужно break; case R.id.center_button: // делайте то, что нужно break; case R.id.right_left_button: // делайте то, что нужно break; case R.id.right_right_button: // делайте то, что нужно break; } } 
    • Just slowly doper at random, which is necessary in the add. place a layer and then your answer) Tell me, why is an empty View used here? - Pollux
    • If there is free space to separate the average image from the left and two right. - iramm
    • Could you suggest any possible reasons why the LinearLayout not stretched the entire width of the toolbar ? On the left, there is an indentation ( screen ), although there is nowhere in the markup, your code copied. - Pollux
    • Try removing the layout_weight attribute from the left ImageButton - iramm
    • No change .. - Pollux

    As for the text in the toolbar suggest replacing your textView with

     <item android:id="@+id/textview_balance" android:title="0" yourapp:showAsAction="always" /> 

    Then in onCreateOptionsMenu remember menu

     public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); this.menu = menu; return true; } 

    And then contact him:

     menu.findItem(R.id.textview_balance).setTitle("155"); 

    Disable the title in the toolbar as follows:

     getSupportActionBar().setDisplayShowTitleEnabled(false); 

    If you need the text for the toolbar center, it is added inside of it, since he is a ViewGroup . For example:

     <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <TextView android:id="@+id/toolbar_title" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Center Title" android:gravity = "center"/> </android.support.v7.widget.Toolbar> 

    In principle, no one bothers to set icons in menu.xml , and text in toolbar.xml . It will be centered in this example by the interval from the left edge to the first icon specified in menu.xml .

    • Thank. Tell me, how can I set the alignment? As in this case, the left button is the button, the center is the image + text, on the right are two buttons. - Pollux
    • @Pollux if only the order of elements is needed, then this is specified in main.xml. You can use getSupportActionBar().setDisplayShowHomeEnabled(true); as the back button getSupportActionBar().setDisplayShowHomeEnabled(true); If it is important that the text be centered in the toolbar , then this is set by toolbar.xml. - ahgpoug
    • @Pollux updated the answer. - ahgpoug
    • Well this only applies if max. the size of one element, and if several, then the porridge is obtained. In principle, it can be solved by adding elements to another layer, for example, RelativeLayout , then the alignment works correctly. - Pollux