Android - Menu

Card Puncher Data Processing

About

UI - Menu (toolbar)

XML Example

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/new_game"
          android:icon="@drawable/ic_new_game"
          android:title="@string/new_game"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/help"
          android:icon="@drawable/ic_help"
          android:title="@string/help" />
</menu>

where:

Java Inflate

If both your activity and fragment(s) declare items for the options menu, they are combined in the UI. The activity's items appear first, followed by those of each fragment in the order in which each fragment is added to the activity. If necessary, you can re-order the menu items with the android:orderInCategory attribute in each <item> you need to move.

Activity

To specify the options menu for an activity, override onCreateOptionsMenu()

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

Fragment

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setHasOptionsMenu(true); // Report that this fragment has a menu and that therefore the onCreateOptionsMenu(Menu, MenuInflater) and related methods will be called back.
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	int id = item.getItemId();
	if (id == R.id.action_refresh) {
		// Do what you want
		return true;
	}
	return super.onOptionsItemSelected(item);
}

Documentation / Reference





Discover More
Card Puncher Data Processing
Android - (App) Resources (R)

The /res subdirectories contain the resources for your application: drawable-/: Directories for drawable resources, other than launcher icons, designed for various densities. layout: Directory...



Share this page:
Follow us:
Task Runner