Frage

I have an Android app that uses ActionBar Sherlock. I create a menu that has an ImageButton whose state is defined in a drawable resource file. (All of them pasted below).

Although I am able to toggle the selected / not selected states of the ImageButton the click listener does not seem to fire.

When the activity is created I inflate the menu, I get the ImageButton and I register the event listener. I debugged and it all seems ok (I am registering the event on the correct ImageButton).

What do you think could cause the ImageButton to not get the onClick callback?

Cheers....

here's some code:

menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menu_save"
          android:showAsAction="always|withText"           
          android:actionLayout="@layout/menu_my_activity"
          />     
</menu>

the menu_my_activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_with_states"
        android:clickable="true" />    
</LinearLayout> 

and the registering of the listener:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = (MenuInflater) this.getMenuInflater();
    inflater.inflate(R.menu.menu_watchlist, menu);
    menu.getItem(0).getActionView().findViewById(R.id.imageButton).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      Log.d("Test", "Hello");   
    }
        });

    return super.onCreateOptionsMenu(menu);
}

Keine korrekte Lösung

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top