How To Make The Toolbar Title Clickable Exactly Like On Actionbar, Without Setting It As Actionbar?
Solution 1:
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 Toolbar
is 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 Toolbar
firing 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
Solution 2:
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(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
Log.d(TAG,"Clicked");
}
});
Solution 3:
You can actually do it pretty easily without having to do any workarounds. You just need to cycle through all the child views of the Toolbar
, and find a TextView
that's got the correct title text.
Stringtitle= toolbar.getTitle();
for(inti=0; i < toolbar.getChildCount(); i++){
ViewtmpView= toolbar.getChildAt(i);
if("android.support.v7.widget.AppCompatTextView".equals(tmpView.getClass().getName())){
if(((AppCompatTextView) tmpView).getText().equals(title)){
tmpView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
//do whatever you want here
}
});
}
}
}
Solution 4:
After setting up not empty toolbar title try this for setting title click listener
if (toolbarView != null) {
intchildCount= toolbarView.getChildCount();
for (inti=0; i < childCount; i++) {
Viewview= toolbarView.getChildAt(i);
if (view instanceof TextView) {
view.setOnClickListener(listener);
break;
}
}
}
and after setting up not empty toolbar subtitle for setting subtitle click listener
if (toolbarView != null) {
intchildCount= toolbarView.getChildCount();
for (inti= childCount - 1; i >= 0; i--) {
Viewview= toolbarView.getChildAt(i);
if (view instanceof TextView) {
view.setOnClickListener(listener);
break;
}
}
}
Post a Comment for "How To Make The Toolbar Title Clickable Exactly Like On Actionbar, Without Setting It As Actionbar?"