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)".
Then, when you click on an item in that list, it is replacing a fragment in my Activity and it set the title to "Action".
When I come back to the first view (always in the same Activity), I reset the title to "My requests (20)", but android decide to truncate it. So the result is "My request...".
After much tests, it looks like that Android is shrinking my title when I put a smaller text in it. But it doesn't enlarge it when I put a longer text, even if there is plenty of room.
What can I do to solve this? I would like a better solution than adding spaces at the end of my short titles :)
Here is the code I use to change the title of the ActionBar :
getActivity().getActionBar().setTitle(title);
You should find that the question I have posted here is the likey problem and I have also posted the solution.
Why my Android ActionBar doesn't update when it is explictily changed
I guess this problem is solved by refreshing action bar UI.
So I had solved with below codes.
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 assupportActionBar
as usual, and it was contained in aCoordinatorLayout
+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):
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 ofinvalidate()
,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:
And this magically solved my truncated titles once and for all.
tl;dr
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.
put
setTitle()
inonCreateOptionsMenu
helps me to solve this problem.In your fragment add
setHasOptionsMenu(true);
inonCreateView(){}
Then override
onCreateOptionsMenu()
.Example:
Reference: How to display android actionbar title without truncation occurring
If anyone is still interested in a proper answer after more than 2 years I encountered this problem as well recently and realized the reason is that I updated the title from background thread. In Kotlin using
supportActionBar?.title = <abc>
.For some reason the app didn't crash as it usually does with an exception mentioning that you are touching views outside of UI thread. Instead it sets new text, but skips layout pass resulting in wrapping the text.
So either use
Activity.runOnUiThread
orView.post
or, when using coroutines as I did, usewithContext(Dispatchers.Main)
to trigger the code that updates the title.Because ListView / GridView / RecyclerView is called notifyDataSetChanged() at the same time. For some reason, this would cause layout update, which I guess result in the chaos of the action bar title's layout update.
Basically, you can postDelay() to update title to fix this problem, but I don't like this way.
I decide to override the BaseAdapter.notifyDateSetChanged() and update item view by hand. It works:
Sorry for my bad english.