Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:34:29+00:00 2026-06-14T15:34:29+00:00

With this code (taken from ABS examples) I’m able to add a Search icon

  • 0

With this code (taken from ABS examples) I’m able to add a “Search” icon in my ActionBar which will display an EditText when clicked (to search in a list):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, searchId, Menu.NONE, R.string.menu_search)
     .setIcon(android.R.drawable.ic_menu_search)
     .setActionView(R.layout.collapsible_edittext)
     .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    return true;
}

With this code I’m able to show a different layout after tapping the “Search” icon:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case searchId:
        search = (EditText) item.getActionView();
        search.addTextChangedListener(filterTextWatcher);
        search.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

        setContentView(R.layout.search);

        this.allSongs = data.getSongs();
        ListView list = (ListView) findViewById(R.id.songList);
        list.setAdapter(new SongAdapter(this, this.allSongs));
    }
    return true;
}

With this code I’m able to handle my search:

private TextWatcher filterTextWatcher = new TextWatcher() {
    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {

        List<Song> results = new ArrayList<Song>();

        Locale locale = Locale.getDefault();

        for (Song song : allSongs) {
            if (song.getTitle().toLowerCase(locale).contains(s.toString().toLowerCase(locale)))
                results.add(song);
        }

        ListView list = (ListView) findViewById(R.id.songList);
        list.setAdapter(new SongAdapter(ctx, results));
    }
};

When the user taps the “Search” icon, the EditText appears in ActionBar and the App Icon gains the “back” left arrow.
If the user taps “Back” button or taps the ActionBar icon (with left arrow), the EditText disappears.
I’d like to intercept this event (no, onOptionsItemSelected() doesn’t fire this time) and show the previous layout, in addition to hiding the EditText.

Note: I need API8 (Android 2.2) compatibility!

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:34:30+00:00Added an answer on June 14, 2026 at 3:34 pm

    When using an ActionView, tapping "up" or "back" doesn’t result in an Ancestral Navigation (which can be handled by onOptionsItemSelected) but results in a Collapsible Action event.

    Found here on Android Developers.

    Handling collapsible action views

    If you need to update your activity based on the visibility of your
    action view, you can receive callbacks when it’s expanded and
    collapsed by defining an OnActionExpandListener and registering it
    with setOnActionExpandListener(). For example:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options, menu);
        MenuItem menuItem = menu.findItem(R.id.actionItem);
        ...
    
        menuItem.setOnActionExpandListener(new OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when collapsed
                return true;  // Return true to collapse action view
            }
    
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                // Do something when expanded
                return true;  // Return true to expand action view
            }
        });
    }
    

    So, to answer to my question, I was searching for onMenuItemActionCollapse event.

    PS: for better consistency I should move my case searchId: from onOptionsItemSelected to onMenuItemActionExpand

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code which is taken from MVC futures and attach the Attribute
I can generate a hexbin diagram like this (code taken from http://matplotlib.org/examples/pylab_examples/hexbin_demo.html ) import
I am using this code taken from here : import smtplib def prompt(prompt): return
This is a code sample taken from the Google Maps API developer's guide. I'm
Is this possible to do the following in ExpressionEngine: (code taken from here )
I don't understand the rationale of this code, taken from javax.swing.event.EventListenerList docs: protected void
Given below is program for encrypting a string. I had taken this code from
In this statement, taken from the Pagerank source code: Pattern.compile(\\[.+?\\]); What does the pattern
This is a part of the syntax highlighting code, taken from TextMate. Do you
Why does this code (taken from the demo sample at jquery ui), doesn't work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.