Search code examples
androidandroid-actionbar

How to add any View (Button, TextView, Spinner) as a menu item to ActionBar using Java in Android Studio?


I want to add any View such as Button, TextView or Spinner to my ActionBar.

How can I achieve this?

An example containing any of the Views will be enough.


Solution

  • If you want to add view in actionbar, you can use toolbar and you can add view in toolbar. For example;

    <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                // Add whatever you want
    
    </android.support.v7.widget.Toolbar>