Android : Add Action Icon / Action button on Action Bar

Points To Remember


  • You need to create a file in res > menu > menu.xml
  • You can use showAsAction property to decide if to show the action icon on action bar or not. 

Adding Action Icon Action button on Action Bar


You can choose either of the following
  • app:showAsAction="never"
    This will show the action in the list view when menu icon is pressed.
  • app:showAsAction="ifRoom"
    This will show the action in the action bar if the amount of space required to show the action is available on the Action bar.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".HomeActivity">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="#Settings"
        app:showAsAction="never" />

    <item
        android:id="@+id/app_search"
        android:orderInCategory="99"
        android:title="#Search"
        app:showAsAction="ifRoom" />

</menu>

Line 7 and 13 tells how to order them in the list.
Line 10 and 16 tells how to show the action in the action bar.

Output will look the following



No comments :

Post a Comment