How to set an icon at the end of Toolbar
Asked Answered
M

5

25

I want to set an icon at the the end of my Toolbar,which start another activity. My Toolbar portion

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/abc_action_bar_default_height_material"
                android:background="#2B4AE0"
                app:theme="@style/ToolBarStyle">

                <TextView
                    android:id="@+id/headerText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@android:style/TextAppearance.Theme"
                    android:textColor="@android:color/white" />

                <RelativeLayout
                    android:id="@+id/notification"
                    android:layout_width="50dp"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:gravity="center" />

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"

                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/bell_icon" />
                </RelativeLayout>
            </android.support.v7.widget.Toolbar>

I tried

android:layout_alignParentEnd="true"

and setting margin left but it doesn't work correctly.

Mark answered 13/1, 2015 at 14:3 Comment(0)
I
45

Try adding this to your ImageView:

android:layout_gravity="end"
Incognizant answered 13/1, 2015 at 14:48 Comment(1)
@Incognizant Works. But when you have items in your menu_main file then this ImageView would be placed to the left of those items. How would you put the ImageView always to the right side (no matter if there are menu items or not)?Keloid
M
19

If you want something like this (icon 2) example

You do not need add icon to the layout manually, you should to implement menu.xml

  1. Create menu.xml like this

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/new_activity"
            android:icon="@drawable/ic_custom_icon"
            android:title="@string/new_activity" />
    </menu>
    
  2. Add it in your activity/fragment to the actionbar/toolbar by

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

Toolbar with menus works fine, it sets icons to the right of toolbar automatically

Metalepsis answered 13/1, 2015 at 14:52 Comment(3)
What if I want to add a custom iconMark
@android_sh_jo add android:icon="@drawable/ic_your_custom_icon" to menu xml, updated code in answerMetalepsis
this will not work if he doesn't want to use supportactionbarNikitanikki
D
8

If there's anyone that still confused (including me before), somehow android studio doesn't provide autocomplete for the android:layout_gravity under the toolbar tag. So to make it works, just copy and paste android:layout_gravity="end" to the ImageView/layout.

Darrendarrey answered 1/5, 2018 at 13:5 Comment(0)
L
3

Setting the orderInCategory will set the order of the menu item

 <item 
    ...
      android:orderInCategory="150"
      app:showAsAction="always"
    ..
    />

if you want to put margin between your menu items you can add an empty menu item in between

Larrainelarrie answered 30/12, 2019 at 10:23 Comment(0)
M
2

<item
    android:title="kjljk"
    app:showAsAction="always"
    android:id="@+id/menuitem_search"
    android:icon="@drawable/pdficon"
    >
</item>

Then in OnCreateView: setHasOptionsMenu(true); Closing the OncreateView below write this: @Override

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.pdf, menu);
    ...
}
Medlock answered 21/11, 2016 at 5:25 Comment(1)
If you are using navigation toolbar then this is the proper way to add iconMedlock

© 2022 - 2024 — McMap. All rights reserved.