askIT

Android Toolbar icons in fragment

action_toolbar_icons_in_fragment.png The Android toolbar was previously known as action toolbar. Now it is only toolbar, and it is avaible in almost all apps on the Google Play.

This article will show you how to add icons to the toolbar in a fragment.

There is also a on click listener for the toolbar icons that has to be programmed. The java source code is also included here.

res\drawable\ic_menu_action_add.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">

    <path
        android:fillColor="#ffffff"
        android:pathData="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
    <path
        android:pathData="M0 0h24v24H0z" />
</vector>

res\drawable\ic_menu_action_delete.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">

    <path
        android:fillColor="#ffffff"
        android:pathData="M6 19c0 1.1 .9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z" />
    <path
        android:pathData="M0 0h24v24H0z" />
</vector>

res\menu\menu_your_menu_file.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <!-- Add -->
    <item android:id="@+id/action_add"
        android:icon="@drawable/ic_menu_action_add"
        android:title="@string/add"
        app:showAsAction="ifRoom"/>

    <!-- Delete -->
    <item android:id="@+id/action_delete"
        android:icon="@drawable/ic_menu_action_delete"
        android:title="@string/delete"
        app:showAsAction="ifRoom"/>

</menu>

java\com.domain.package\yourFragment.class

Add a method for onActivityCreated, onCreateOptionsMenu and onOptionsItemSelected.

/*- on Activity Created --------------------------------------------------------- */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);

    // Create menu
    setHasOptionsMenu(true);
} // onActivityCreated

/*- on Create Options Menu --------------------------------------------------------- */
// Creating action icon on toolbar
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate menu
    ((MainActivity)getActivity()).getMenuInflater().inflate(R.menu.menu_your_menu_file, menu);
}

/*- on Options Item Selected -------------------------------------------------------- */
// Action icon clicked on
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {

    int id = menuItem.getItemId();

    if (id == R.id.action_add) {
        // Do something
    }
    else if (id == R.id.action_delete) {
        // Do something
    }
    return super.onOptionsItemSelected(menuItem);
}

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.