Skip to content

Instantly share code, notes, and snippets.

@surajsau
Last active August 6, 2020 16:39
Show Gist options
  • Save surajsau/940e5719a13a3916cdc4d2ada827c422 to your computer and use it in GitHub Desktop.
Save surajsau/940e5719a13a3916cdc4d2ada827c422 to your computer and use it in GitHub Desktop.
AOSP implementation of Toolbar.setTitle()
/**
* Set the title of this toolbar.
*
* <p>A title should be used as the anchor for a section of content. It should
* describe or name the content being viewed.</p>
*
* @param title Title to set
*/
public void setTitle(CharSequence title) {
if (!TextUtils.isEmpty(title)) {
if (mTitleTextView == null) {
final Context context = getContext();
mTitleTextView = new AppCompatTextView(context);
mTitleTextView.setSingleLine();
mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
if (mTitleTextAppearance != 0) {
mTitleTextView.setTextAppearance(context, mTitleTextAppearance);
}
if (mTitleTextColor != null) {
mTitleTextView.setTextColor(mTitleTextColor);
}
}
if (!isChildOrHidden(mTitleTextView)) {
addSystemView(mTitleTextView, true);
}
} else if (mTitleTextView != null && isChildOrHidden(mTitleTextView)) {
removeView(mTitleTextView);
mHiddenViews.remove(mTitleTextView);
}
if (mTitleTextView != null) {
mTitleTextView.setText(title);
}
mTitleText = title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment