Good day. It is required to make a menu in the toolbar :
So far it turns out not quite that)
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> 
