1

i have this code in my main activity

public boolean onOptionsItemSelected(MenuItem item) {

   int itemId = item.getItemId();

   if (itemId == R.id.menu_edit) {

        CharSequence title = item.getTitle();
        Log.d("onOptionsItemSelected", "menu_edit title=" + title);
        if(title=="Edit") {
            // switch to RUn
            item.setTitle("Run");
        } else {
            // switch to Edit
            item.setTitle("Edit");
        }
    }

    return super.onOptionsItemSelected(item);

}

the button is define like this

    android:id="@+id/menu_edit"
    android:orderInCategory="102"
    android:showAsAction="ifRoom|withText"
    android:title="Edit"/>

the basic idea is the "Edit" button toggles to "Run" and back when pressed. it kind of works except for the first time i press it it does nothing, then it works ok: note this is a item on the action bar, not in the menu so i don't think onPrepareOptionsMenu helps since its not called when pressing buttons on the action bar.

can someone suggest a way to get the first click to for setTitle to work?

thanks

steveh
  • 1,352
  • 2
  • 27
  • 41

2 Answers2

2

Replace:

if(title=="Edit") {

with:

if(title.equals( "Edit" ) ) {

and it will work the first time too.

sschrass
  • 7,014
  • 6
  • 43
  • 62
Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
  • because of the difference between == and equals applied to strings. Take a look at this question http://stackoverflow.com/questions/767372/java-string-equals-versus – Robert Estivill Jan 07 '13 at 01:03
0

Better to use the equalIgnoreCase instead of the equals as equalignorecase ignores the case of cps on/off button...

deepak825
  • 432
  • 2
  • 8