Toni
Toni

Reputation: 1852

Android Actionbar: click Up-Button programmatically

Hi,
Android 4 (ICS) offers a view element called ActionBar. Using the ActionBar, I've created a search widget where I can enter text to trigger a remote suggest search. If you click on a magnifier icon within this ActionBar to open the search widget textfield, two things happen:

  1. The softkeyboard gets displayed
  2. The "home button" (app icon on the top left) changes his functionality to an "up button". "up" means, that you undo your last action (in this case: opening the search widget textfield)

If you click on the "up button" the search widget collapses so that the textfield disappears. In the same moment the "up button" changes it's functionality back to act as a "home button". This behaviour describes the "default" behavior of the Android 4 ActionBar with an attached search widget.

Using the hardware "back button" instead of the soft "up button", the keyboard disappears, but the search widget remains open. Also the "up button" doesn't change to a "home button".

My question: How can I trigger a "up button" click within the ActionBar programmatically? If I could do this, I could collapse the search widget textfield and put back the "up button" to become a "home button" again, if the user presses the hardware "back button".

This example shows a "home button" on the very left and a closed search widget "magnifier icon" on the right. If the user clicks the magnifier, the textfield shows up and the "home button" becomes a "up button".

Thanks in advance!

enter image description here

Upvotes: 4

Views: 3736

Answers (3)

Swr7der
Swr7der

Reputation: 859

I think there can be a solution for this. As documentation says,

To navigate up when the user presses the app icon, you can use the NavUtils class's static method, navigateUpFromSameTask().

So you can manually call

NavUtils.navigateUpFromSameTask(this);

for performing home button click programmatically.

Upvotes: 3

LuxuryMode
LuxuryMode

Reputation: 33741

You can trigger a click on any view programmatically by calling performClick() on the View.

See the performClick() documentation.

Upvotes: -1

Christian Smith
Christian Smith

Reputation: 615

I have scoured the net and SO, and can't find any way to fire this programmatically. I ended up just recreating the bits and pieces needed to make the view switch back to the home view along with changing the actionbar back to normal and putting that in a public method so other classes can make the view go home.

Upvotes: 0

Related Questions