Spike
Spike

Reputation: 147

Way to set Home Application Icon in ActionBarSherlock

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?

Upvotes: 2

Views: 1874

Answers (1)

Alex Fu
Alex Fu

Reputation: 5529

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.

Upvotes: 2

Related Questions