How to set the icon at the end of the toolbar

I want to set an icon at the end of the Toolbar , which will start another action. My Toolbar

  <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've tried

 android:layout_alignParentEnd="true" 

and leave a margin, but it does not work properly.

+7
android android-actionbar android-toolbar
source share
4 answers

Try adding this to your ImageView:

 android:layout_gravity="end" 
+16
source share

If you want something like this (icon 2) example

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

  • 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> 
  • Add it to your activity / snippet in the action bar / toolbar on

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

The toolbar with the menu works fine, it automatically sets icons to the right of the toolbar.

+7
source share

If anyone who is still confused (including me before), somehow the android studio does not provide autocomplete for android:layout_gravity under the toolbar tag. To make it work, just copy and paste android:layout_gravity="end" into ImageView / layout.

+1
source share

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

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

 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.pdf, menu); ... } 
-one
source share

All Articles