0

I want to show save button when edit button clicked and hide edit button

And

show edit button when save button is clicked and hide save button.

My menu file is as below:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
    android:id="@+id/edit_button"
    android:icon="@drawable/edit_button"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/edit"/> 

    <item
    android:id="@+id/save_button"
    android:icon="@drawable/save_button"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/save"/> 

</menu>

Screenshot for reference :

enter image description here

tshepang
  • 12,111
  • 21
  • 91
  • 136
Ponting
  • 2,248
  • 8
  • 33
  • 61
  • Try to use change background of Edit/Save like ToggleButton. – Bhoomika Brahmbhatt Jul 03 '13 at 13:01
  • @geet changing the background would be a bad practice, since you will have to ask for what background is being displayed in `onOptionsItemSelected()` for know what action to do `save` or `edit`, I think having a save MenuItem and an edit MenuItem by separate is a better approach for an easy maintenance of code. – jac1013 Jul 03 '13 at 13:18

2 Answers2

0

You must do this programmatically like here add: Dynamic control of action/menu items in ActionBar, delete: How do I hide a menu item in the actionbar?, you could use a flag and then call invalidateOptionsMenu() to update the changes you made on the Menu items, i don't have an example right now but i think with this explanation you should achieve what you are trying to do. Another way could be add both MenuItem in method onCreateOptionsMenu (or directly via UI XML) and then in method onPrepareOptionsMenu hide the edit button and show the save button with setVisible() method update with the method mentioned before, sorry for my english, still learning it.

Community
  • 1
  • 1
jac1013
  • 408
  • 3
  • 11
0

I used ViewSwitcher to switch between two different buttons..

Ponting
  • 2,248
  • 8
  • 33
  • 61