johannilsson / android-actionbar

DEPRECATED Android Action Bar Implementation
1.31k stars 564 forks source link

Clicking logo does not trigger onOptionsItemSelected #29

Open LorneLaliberte opened 13 years ago

LorneLaliberte commented 13 years ago

The documentation for ActionBar.NAVIGATION_MODE_STANDARD states:

    /**
     * Standard navigation mode. Consists of either a logo or icon and title
     * text with an optional subtitle. Clicking any of these elements will
     * dispatch onOptionsItemSelected to the host Activity with a MenuItem with
     * item ID android.R.id.home.
     */

...however this does not seem to occur in my app. The highlight colour is shown, but it does not reach either onOptionsItemSelected or onMenuItemSelected.

The same thing occurs in both NAVIGATION_MODE_STANDARD and NAVIGATION_MODE_LIST.

Note also that android.R.id.home is not resolvable in 2.3.3 (API level 10), it's only available since 3.0 (API level 11 or higher).

johannilsson commented 13 years ago

The documentation needs to be updated on this, the id should be R.id.actionbar_item_home instead of android.R.id.home. Clicks on the title is supported via setOnTitleClickListener.

This patch seems to do the trick for the logo, please review it and see how it works for you.

diff --git a/actionbar/src/com/markupartist/android/widget/ActionBar.java b/actionbar/src/com/markupartist/android/widget/ActionBar.java
index dcf40f2..13c44b7 100644
--- a/actionbar/src/com/markupartist/android/widget/ActionBar.java
+++ b/actionbar/src/com/markupartist/android/widget/ActionBar.java
@@ -378,6 +378,14 @@ public class ActionBar extends RelativeLayout {
             mHomeUpIndicator.setVisibility(getDisplayOptionValue(DISPLAY_HOME_AS_UP) ? View.VISIBLE : View.GONE);
             mHomeLogo.setVisibility(usingLogo ? View.VISIBLE : View.GONE);
             mHomeView.setVisibility(usingLogo ? View.GONE : View.VISIBLE);
+
+            if (usingLogo) {
+                Action homeAction = findAction(R.id.actionbar_item_home);
+                if (homeAction != null) {
+                    mHomeLogo.setTag(homeAction);
+                    mHomeLogo.setOnClickListener(mActionClicked);
+                }
+            }
         } else {
             mHomeUpIndicator.setVisibility(View.GONE);
             mHomeLogo.setVisibility(View.GONE);

This is how I configured the action bar:

ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setHomeLogo(R.drawable.logo_title);
actionBar.setHomeAction(actionBar.newAction(R.id.actionbar_item_home));
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);

The normal icon should work as intended, you can see an example of it here:

https://github.com/johannilsson/sthlmtraveling/blob/master/src/com/markupartist/sthlmtraveling/BaseActivity.java#L43

JakeWharton commented 13 years ago

API documentation for the methods whose signatures match the native action bar have been copied verbatim from Android 3.1 so there may be a few differences. It should definitely be given a looking over before merging to master.

LorneLaliberte commented 13 years ago

@johannilsson, yep that patch takes care of both issues here (tested in NAVIGATION_MODE_LIST).

Thanks!

@JakeWharton, yeah I recognized some of that text from http://developer.android.com/guide/topics/ui/actionbar.html. :)

adiktz commented 12 years ago

That patch worked...!!! Thanks...!!!

adiktz commented 12 years ago

One more thing...!!! Can I disable click on the home icon...??? I mean it should not show the changed background when it is pressed...!!!