0

I want to create a menu item in this Toolbar view, but I didn't use what to create it? enter image description here

SOLVE SELF:

 <com.google.android.material.button.MaterialButton
                             android:id="@+id/image_quote"
                             android:layout_width="30dp"
                             android:layout_height="30dp"
                             app:icon="@drawable/ic_fi_br_quote_right"
                             app:iconTint="@color/colorBlack"
                             app:iconSize="13dp"
                             app:cornerRadius="15dp"
                             android:layout_margin="3dp"
                             android:insetLeft="0dp"
                             android:insetTop="0dp"
                             android:insetRight="0dp"
                             android:insetBottom="0dp"
                             app:iconGravity="textStart"
                             app:iconPadding="0dp"
                             style="@style/ShapeB" />
T6VK
  • 65
  • 1
  • 7

1 Answers1

1

You can simply do that just by creating a menu in the menu directory. Then add that menu to the toolbar using OnCreateOptionMenu and control using OnOptionItemSelectedListener .

In the activity's onCreate method, set the toolbar with

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Then add this method.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.yourmenufile, menu);
  return super.onCreateOptionsMenu(menu);
}

Then add menu click listener,

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case 0:
            // do whatever
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Credit: How can we set menu to toolbar

K M Rejowan Ahmmed
  • 1,034
  • 2
  • 12
  • 26