---
# System prepended metadata

title: Add action button in fragment page

---

# Add action button in fragment page
1. Define a 'action_menu.xml'
```xml=
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/action_save" android:title="Save" app:showAsAction="always"/>

</menu>
```
2. SetSupportActionBar(This is important)
```kotlin=
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_menu)

        setSupportActionBar(menu_toolbar)
    }
```
3. SetHasOptionsMenu in fragment class
```kotlin=
override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        setHasOptionsMenu(true)

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sign, container, false)
    }
```
4. Override onCreateOptionsMenu function
```kotlin=
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {

        inflater.inflate(R.menu.action_menu,menu)
        super.onCreateOptionsMenu(menu, inflater)
    }
```
5. Override onOptionsItemSelected function
```kotlin=
 override fun onOptionsItemSelected(item: MenuItem): Boolean {

        if(item.itemId == R.id.action_save){

            showMessage("Data has been saved")
            findNavController().popBackStack()
        }

        return super.onOptionsItemSelected(item)
    }
```