android developer android developer - 30 days ago 20
Android Question

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

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 Source

Just remember that the Toolbar is basically just a ViewGroup so you can add a TextView to it and listen to onClick events like that.

Add TextView to Toolbar in XML:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Settings"
        android:id="@+id/toolbar_title" />

    </android.support.v7.widget.Toolbar>

Listen for clicks in your Activity:

toolbarTop.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"Clicked");
        }
    });