Adam
Adam

Reputation: 33146

Automatically expand ActionView when fragment is displayed

I want something very simple (which IMHO ought to be the default anyway!):

[logo] [---------------expanded action view--------------] [other actionview]

Android by default does this:

[logo] [----stupid empty space----][collapsed action view] [other actionview]

Fine, I thought: let's just taken the MenuItem, and call "expandActionView()" in onCreateOptionsMenu.

Nope!

Android now does this:

[back symbol] [logo] [---------------expanded action view--------------] [other actionview]

But ...where did that back come from? Why is it a button? What is it doing?

  1. home-as-up is disabled: getActionBar().setDisplayHomeAsUpEnabled(false);
  2. home is disabled: getActionBar().setHomeButtonEnabled(false);
  3. If you tap it ... Android DELETES THE ACTIONVIEW COMPLETELY. Huh?

Is this a bug in Android? Two bugs, even? I can't find any mention of this magic behaviour in the docs, and the bit where it deleted the actionview seems very, very wrong :(. Should calling "expandActionView()" really deleted the ActionBar configuration and replace with a new one?

Upvotes: 3

Views: 1053

Answers (1)

Adam
Adam

Reputation: 33146

Seems to be as-designed, but the design is poor.

  1. The "unavoidable" home button is because it's hard-coded to assume that every "expand" was triggered by the user hitting a collapsed view and asking it to expand (obviously this is often NOT the case if you're doing it programmatically). It does NOT appear to use the back-stack either, which is odd

  2. The "disappaearing" view is because I had removed the icon from my actionview (it was supposed to be always-expanded), and when hitting the home button it de-expanded it.

Workaround:

  1. Don't use MenuItem.collapseActionView / .expandActionView - they are badly designed, don't work properly, and aren't supported on most Android versions!
  2. Instead:
    1. Use custom ActionLayouts
      1. NB: onOptionsItemSelected is broken: it doesn't detect taps on the custom actionview
      2. NB: setOnMenuItemClickListener is broken: it doesn't detect taps on the custom actionview
    2. Add an onClickListener to each ActionView inside your onCreateOptionsMenu
    3. Use View.setVisibility( View.GONE ) to collapse items
      1. Have a "collapsed" view and an "expanded" view in the same layout.
      2. At all times, have one of them as GONE, and the other as VISIBLE

NB: essentially this means re-implementing the whole of ActionView collapse/expand, with the following differences:

  1. If you do it yourself, it works
  2. It works for all versions of Android
  3. The layout is correct (Google's layout calculation for ActionBar expanded ActionViews is known to be slightly buggy, because the algorithm for deciding "how much space" isn't correct, and ignores the actual size of the title + app-icon)

Upvotes: 2

Related Questions