How to Display Both Icon and Title of Action Inside Actionbar

ActionBar with icon & title

When I want to replace the action bar with toolbar in all activities I would override setContentView() method in BaseActivity. It looks like this:

    @Override
public void setContentView(@LayoutRes int layoutResID) {
super.setContentView(layoutResID);
View view = findViewById(R.id.toolbarContainer);
if (view != null) {
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
setTitle(getTitle());
}
}

Add Icon to the left of the title in the action bar in android

Recommended way

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_settings_24dp);// set drawable icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Handle icon click event

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "click..!!", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);

}
}

Show both logo and app name in action bar android

Solved adding

    getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.icona);
getSupportActionBar().setDisplayUseLogoEnabled(true);

Using logo in actionbar is disabled by default in Android 5.0 Lollipop.



Related Topics



Leave a reply



Submit