2

I am confused the way to set home icon on ActionbarSherlock and of course am new to this ActionBarSherlock. Have checked many sources, but unable to get how to set the home icon. Below is my class that sets the ActionbarSherlock.

public abstract class BaseActivity extends SherlockActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem miPrefs = menu.add("Login");
        miPrefs.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        miPrefs.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent loginIntent = new Intent(BaseActivity.this, LoginForm.class);
                startActivity(loginIntent);
                return true;
            }
        });
        return true;
    }
}

Of course I know how to set application icon as home icon in the normal action bar. The following is the way I usually set the normal actionbar.

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuItem menu1 = menu.add(0, 0, 0, "Login");
    menu1.setIcon(R.drawable.image1);
    menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}

In the onCreate(), we have to get the actionbar by getActionBar() and then with actionbar.setDisplayHomeAsEnabled(true), it is possible to set the application icon as home icon. By setting the following we can listen to the clicks of home icon.

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case android.R.id.home:
        // Here we can keep the code to get to the mainactivity.
        return true;
    }
}

Also, when I just try to get the actionbar by ActionBar actionbar = getSupportActionBar(); in oncreate(), I get this error,

Type mismatch: cannot convert from com.actionbarsherlock.app.ActionBar to android.app.ActionBar

I'm confused about how to set the application icon as home icon based on the above code of ActionbarSherlock and listen for the clicks. How can I get that done?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Spike
  • 147
  • 3
  • 17

1 Answers1

2

Enabling the App Icon to be clickable in the ActionBar (using ABS)

@Override
public void onCreate() {
    super.onCreate();
    getSupportActionBar().setHomeButtonEnabled(true);
}

ABS is a library so when you want to access it's features, you must use it's own methods/classes, not to be confused with the default Android methods/classes (such as getActionBar() and getSupportActionBar()). A great place for sample code is https://github.com/JakeWharton/ActionBarSherlock/tree/master/samples/demos.

Listening to clicks

The same as what you have above.

Alex Fu
  • 5,509
  • 3
  • 31
  • 40
  • Thank you so much for your answer and +1 for your time. Yeah I somewhat know this, but could you please explain me the way to set listener to the clicks of app icon based on my BaseActivity class that was provided in my question? I am able to listen to the clicks of action items as I kept in the code. But how to access app icon and set listeners to it. – Spike Dec 08 '12 at 08:29
  • Please see my above code in Base Activity and see my code in the onOptionsItemSelected() in the normal action bar code. In the normal action bar I can find home button by case android.R.id.home. But how it is possible in my above BaseActivity class? – Spike Dec 10 '12 at 12:17