59

I added android.support.v7.widget.Toolbar in my app using below code, now I want to show a button in the right end of the toolbar, but not able to do so.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/accent_color"
    android:minHeight="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    tools:context=".MyActivity"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/showevents"
        android:textSize="12sp"
        android:background="@null"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:textColor="@color/white"
        android:text="UPCOMING \nEVENTS"/>
</android.support.v7.widget.Toolbar>

I have added the below shown too but its not getting moved to right.

android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"

Attached image for reference:

enter image description here

Psypher
  • 10,717
  • 12
  • 59
  • 83

3 Answers3

143

You should add android:layout_gravity="end" for your Button :

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:id="@+id/showevents"
        android:textSize="12sp"
        android:background="@null"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:textColor="#FFF"
        android:text="UPCOMING \nEVENTS"/>

enter image description here

mt0s
  • 5,781
  • 10
  • 42
  • 53
  • Better set the style of the button to `style="@style/Widget.AppCompat.Button.Borderless"` or use `background="?android:attr/selectableItemBackground"` instead of a @null background to have a ripple effect when touched otherwise the button seems disabled. – Roel Mar 11 '16 at 15:12
  • 1
    Tip: `android:textAllCaps="true"` could be used instead of writing all caps `android:text` yourself. Material themed buttons automatically include it, but on older devices it's normal to have non-uppercase buttons. – TWiStErRob Apr 06 '16 at 11:04
  • 1
    @Roel, `?selectableItemBackgroundBorderless` to have them both. – WindRider Nov 02 '17 at 13:46
  • Use "end" instead of "right" (That's what Android Studio is warning me in 2018) – GabrielBB Nov 18 '18 at 07:15
  • What if I add one view programmatically, In my case View v1 = inflate(R.layout.view1); v1.addRule(ALIGN_END, TURE); getToolbar().addView(v1); does not work. please help, thx. – thecr0w May 06 '19 at 06:15
9

Or for image on the top right:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="Edit Note">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:id="@+id/submitEditNote"
        android:src="@android:drawable/ic_menu_send"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true" />
</android.support.v7.widget.Toolbar>

I hope it helps

Gilad Brunfman
  • 3,452
  • 1
  • 29
  • 29
2

Your way is right but take a look at official doc: (Resourcs)

Create menu of items:

<!-- "Mark Favorite", should appear as action button if possible -->
<item
    android:id="@+id/action_favorite"
    android:icon="@drawable/ic_favorite_black_48dp"
    android:title="@string/action_favorite"
    app:showAsAction="ifRoom"/>

<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      app:showAsAction="never"/>

Add menu action by ID

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            // User chose the "Settings" item, show the app settings UI...
            return true;

        case R.id.action_favorite:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;

        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);

    }
}
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60