android-actionbar icon indicating copy to clipboard operation
android-actionbar copied to clipboard

Clicking logo does not trigger onOptionsItemSelected

Open LorneLaliberte opened this issue 14 years ago • 5 comments

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).

LorneLaliberte avatar Jun 01 '11 08:06 LorneLaliberte

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

johannilsson avatar Jun 01 '11 09:06 johannilsson

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.

JakeWharton avatar Jun 01 '11 11:06 JakeWharton

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

  • I actually have no pressing need to use the logo, I was just testing a static helper method.

Thanks!

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

LorneLaliberte avatar Jun 01 '11 12:06 LorneLaliberte

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

adiktz avatar Jan 11 '12 23:01 adiktz

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

adiktz avatar Jan 11 '12 23:01 adiktz