How to make the Toolbar title clickable exactly like on ActionBar, without setting it as ActionBar?

android developer picture android developer · Oct 21, 2014 · Viewed 37.3k times · Source

Background

I wish to show an ActionBar on PreferenceActivity, as it's not available for pre-Honeycomb devices (even in the support library).

Because such a class isn't available on the support library, I can't just call "setSupportActionBar" . I also don't wish to use a library (like this one) since all of the libraries I've found still use Holo style, so they didn't get updated so far, with this in mind.

To do this, I'm trying to mimic the look&feel of the title of the ActionBar into the new Toolbar class, that was presented on support library v7 .

The problem

I've succeeded putting a title and a an "up" button, but I can't find out how to make them clickable like a normal action bar, including the effect of touching.

The code

Here's the code:

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity
  {
  @Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    addPreferencesFromResource(R.xml.pref_general);
    final Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
    toolbar.setBackgroundColor(getResources().getColor(R.color.windowBackgroundColor));
    toolbar.setTitle("Settings");
    toolbar.setClickable(true);
    toolbar.setLogo(getResIdFromAttribute(this,R.attr.homeAsUpIndicator));
    }

  public static int getResIdFromAttribute(final Activity activity,final int attr)
    {
    if(attr==0)
      return 0;
    final TypedValue typedvalueattr=new TypedValue();
    activity.getTheme().resolveAttribute(attr,typedvalueattr,true);
    return typedvalueattr.resourceId;
    }
  }

activity_settings.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.antonioleiva.materialeverywhere.SettingsActivity" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/abs__ab_solid_shadow_holo" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

</LinearLayout>

What I've tried

I've tried all of the click listeners types of the Toolbar class, but none helped.

The only thing that almost worked is to use "setNavigationIcon" instead of "setLogo", but that actually makes the title&icon being clicakable, yet only the icon shows its effect of being clicked (even if I click on the title).

The question

How can I add the look & feel of the Toolbar, to have the title & logo of it to look like the ActionBar?

Answer

prom85 picture prom85 · Nov 4, 2015

I had the same problem and found a quite convenient way for it (for the clickable title), doing this with the Toolbar. It does not matter, if the Toolbaris used as ActionBar or not, it works for both cases.

Just set the click listener on the Toolbar itself, this will result in the complete Toolbarfiring the click listener, if not a menu item or the home icon is clicked. For me, that's exactly what I expect...

For example:

 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getThis(), "Toolbar title clicked", Toast.LENGTH_SHORT).show();
        }
    });

onOptionsItemSelected, home click and similar WON'T fire the onClick event. Seems like a good solution