Android: ¿cambiar dinámicamente el icono de ActionBar?

I would like to dynamically change the "home" icon in the ActionBar. This is easily done in v14 with ActionBar.setIcon(...), but I can't find anyway to accomplish this in previous versions.

preguntado el 01 de febrero de 12 a las 03:02

Does anything happen when you call setIcon? And if you're trying to accomplish things in anything pre-Honeycomb, you should take a look at ActionBarSherlock. -

I am using a customized ActionBar-esque component for pre-v11 (similar to ActionBarSherlock). This is for v11-v13 (honeycomb). When I call setIcon in honeycomb, I get a methodnotfound exception. -

setIcon is only available in API levels >= 14 -

4 Respuestas

If your actionbar works like Sherlock and is based on menu items, this is my solution:

public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem switchButton = menu.findItem(;     
    return super.onPrepareOptionsMenu(menu);


Respondido 09 Feb 12, 03:02

well, for honeycomb I use the actionbar provided by the sdk. so, I'm trying to find a honeycomb equivalent of the ActionBar.setIcon() method provided in v14. - ab11

Thanks for this great snippet - been looking for this for a while! - aaronsnoswell

it works for all items. But I get a crash with nullpointer exception if I use it for the home icon ( - tobias

Si está utilizando el ActionbarCompat code provided by google, you can access the home icon via the class for API v4 onwards.

    //code snippet from
    private void setupActionBar() {
    final ViewGroup actionBarCompat = getActionBarCompat();
    if (actionBarCompat == null) {

    LinearLayout.LayoutParams springLayoutParams = new LinearLayout.LayoutParams(
            0, ViewGroup.LayoutParams.MATCH_PARENT);
    springLayoutParams.weight = 1;

    // Add Home button
    SimpleMenu tempMenu = new SimpleMenu(mActivity);
    SimpleMenuItem homeItem = new SimpleMenuItem(tempMenu,
  , 0, mActivity.getString(R.string.app_name));

    // Add title text
    TextView titleText = new TextView(mActivity, null,

You should be able to modify the code to the home button accessible to the activities that extend ActionBarActivity and change it that way.

Honeycomb seems a little harder and it doesn't seem to give such easy access. At a guess, its id should also be so you may be able to pull that from the view in

Respondido 26 Oct 12, 03:10

¡Si! Gracias. Activity.findViewById( obras. - ab11

I would say you do something like this :


ver el enlace How to change the icon actionBarCompat

Respondido 11 Abr '15, 19:04

The ActionBar will use the android:logo attribute of your manifest, if one is provided. That lets you use separate drawable resources for the icon (Launcher) and the logo (ActionBar, among other things).

Respondido 23 ago 13, 18:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.