Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show my Spinner to right of Toolbar

my Layout looks like below enter image description here

You can see spinner added to my toolbar, but what I want is to make it right align, show it to most right of toolbar.

below is the xml code I used

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:elevation="4dp"
            android:gravity="right"                          //gravity set to right
            android:background = "@color/color_toolbar"
        >
             <Spinner
                android:id="@+id/spinner_category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar> 
   </LinearLayout>

spinner is not showing android:layout_gravity enter image description here I tried setting gravity to right but it doesn't work. how can I do this?

like image 856
karan Avatar asked Aug 05 '15 12:08

karan


People also ask

How do I add a spinner to my toolbar?

Adding spinner to app bar/ toolbar is very simple, you just need to create a XML file in res/menu/ folder and add a item like your over flow menu and spinner widget as item actionViewClass, rest in your java code. Spinner can be added to android actionbar/toolbar with many ways.


1 Answers

I had the same issue. I have fixed the alignment issue based on the comments of question. So keeping the answer here to help someone directly.

<?xml version="1.0" encoding="utf-8"?>
<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="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">
    <Spinner
        android:id="@+id/toolbar_spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:visibility="visible"
        android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>
like image 119
Noundla Sandeep Avatar answered Sep 28 '22 11:09

Noundla Sandeep