I have create an Android application using NavigationDrawer
. When the application is created, there is one icon menu set by default, placed in the left of the toolBar. And my aim is to add another icon placed, for example, in the right of the toolbar. I have tried many tutorials by following them step by step, by I can't achieve it. Could anyone help me?
You need to create a menu to achieve this task. For example;
res/menu/menu_main.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/new_item"
android:icon="@drawable/new_icon"
app:showAsAction="ifRoom">
</item>
</menu>
If you already have a menu then you can continue with it.
After you have created the menu, you need to inflate it.
public class MainActivity extends AppCompatActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
Refer this for more details about using the menu resource: https://developer.android.com/guide/topics/resources/menu-resource