In my app, I change the title in the ActionBar from each fragment displayed. When I first start my apps, I got a list of requests, so my title is \"My requests (20)\".
T
A question from 2013, still being a thing.
Well, for those still shocked to occasionally see this happening even though you're happily using Androidx, all recent artifacts, etc. I found something that fixed it and it doesn't require custom titles, or anything special.
Bear in mind that this may not be your case but it appears to have magically fixed this issue in an instant.
In my case, the Toolbar
, was set as supportActionBar
as usual, and it was contained in a CoordinatorLayout
+ AppBar
. At first I was going to blame those, because why not, but before doing so I decided to inspect the Toolbar's source code and debug what was happening.
I couldn't tell (but I did reach the code that was causing this ellipsis):
/**
* Set the title of this toolbar.
*
* A title should be used as the anchor for a section of content. It should
* describe or name the content being viewed.
*
* @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;
}
This is straight from the latest Toolbar.java in appCompat 1.1.0
(latest at this time in October 2019).
Nothing special, but you can see that when the TitleTextView is first created, it's set to singleLine and to use an Ellipsize at the END... (or right in LTR languages).
However, I noticed that when I was calling setTitle, the code didn't go thought that, presumably because the titleTextView was created before (and at that time, set to singleLine with Ellipsis); when I called setTitle, all the code was doing was mTitleTextView.setText(title)
. No amount of invalidate()
, forceLayout()
, requestLayout()
to the toolbar would fix this.
Upon further inspection, I noticed that my layout (the Coordinator layout), was contained in a RelativeLayout
.
The view Hierarchy looked like:
I never liked RelativeLayout, I thought it was a horrible design to begin with, but also faced various issues with it since I can remember, and that it was very bad at handling its children.
For fun, I decided: "what if I change this RelativeLayout into a ConstraintLayout?"
I "right clicked" in the RelativeLayout in the Android Studio visual editor -> convert to ConstraintLayout, next, finish (Didn't touch the defaults).
Now it looks like this:
Same, but the ROOT is no longer a relative layout.
I made two manual changes to the generated XML:
This is how the CoordinatorLayout looked after the conversion:
It looked right in the preview, but it caught my attention because one _should not use match_parent
in a child of ConstraintLayout... so... I changed it to:
Which is exactly the same I had before (the coordinator layout taking up the whole screen).
This is thanks to the new ConstraintLayout having these:
android:layout_width="match_parent"
android:layout_height="match_parent"
And this magically solved my truncated titles once and for all.
Do not use RelativeLayouts anymore in 2019-onward, they are bad from a lot of points of view. If your toolbar is contained in a relative layout, consider migrating to a ConstraintLayout or any other ViewGroup.