Reputation: 1
I have the default AppBar layout inside a coordinator layout
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="@dimen/activity_horizontal_margin"
app:expandedTitleMarginEnd="@dimen/activity_horizontal_margin"
app:expandedTitleGravity="start|bottom"
app:collapsedTitleTextAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FAFAFA"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:layout_collapseMode="parallax">
<com.petronas.laniakea.view.FixedRatioImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/logo_blue" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
How do I set an OnClickListener for the title TextView? I was able to get the Toolbar private Title TextView, make it public and set an OnClickListener to that view:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Field titleField = null;
try {
titleField =Toolbar.class.getDeclaredField("mTitleTextView");
titleField.setAccessible(true);
TextView barTitleView = (TextView) titleField.get(toolbar);
barTitleView.setOnClickListener(v -> {});
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
The code above works fine but I still don't get a click event when I click the title. Any Ideas? Here is my view hierarchy as it appears on Stetho: View Hierarchy
Upvotes: 0
Views: 965
Reputation: 364
As far as I know, there is no way to set an OnClickListener on the TextView in any sort of an ActionBar or toolbar. The way you are using uses reflection APIs, which, when packaged in a release app, will promptly crash.
Upvotes: 2
Reputation: 3815
You can try by wrapping the TextView
by Toolbar
. So, your layout structure be like:
<Toolbar ...>
<TextView android:id="@+id/toolbarTextId" ..../>
</Toolbar>
Now, use OnClickListener
for tap on text.
yourTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// some action
}
});
Upvotes: 0