Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android v7 Toolbar button alignment

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

like image 323
Psypher Avatar asked Feb 03 '15 13:02

Psypher


2 Answers

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

like image 110
mt0s Avatar answered Sep 20 '22 15:09

mt0s


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

like image 24
Gilad Brunfman Avatar answered Sep 21 '22 15:09

Gilad Brunfman