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.
*
* <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;
}
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:
<RelativeLayout>
<CoordinatorLayout>
<AppBarLayout>
<androidx.appcompat.widget.Toolbar />
</AppBarLayout>
<androidx.viewpager2.widget.ViewPager2 />
<com.google.android.material.bottomnavigation.BottomNavigationView />
</CoordinatorLayout>
</RelativeLayout>
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:
<ConstraintLayout>
<CoordinatorLayout>
<AppBarLayout>
<androidx.appcompat.widget.Toolbar />
</AppBarLayout>
<androidx.viewpager2.widget.ViewPager2 />
<com.google.android.material.bottomnavigation.BottomNavigationView />
</CoordinatorLayout>
</ConstraintLayout>
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:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
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:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
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.
Your AndroidManifest.xml has your activity definition like so
<activity
android:name="com.example.DetailsView"
android:label="Details">
</activity>
If you change android:label="Whatever"
to android:label=" "
,
It'll work. I assume this happens because android creates a TextView to display label and the width is not re-sized when setTitle is called. So by setting the initial title to have more characters than you're going to set the title with, it will not be truncated.
I will add that you can totally use the action bar title on 2 lines to get more space:
//Set the title over 2 lines if needed:
int titleId = Resources.getSystem()
.getIdentifier("action_bar_title", "id", "android");
if (titleId > 0) {
TextView title = (TextView) findViewById(titleId);
title.setSingleLine(false);
title.setMaxLines(2);
// title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
}
I am using it on my application and I am happy with it :)
Credit to this post
I guess this problem is solved by refreshing action bar UI.
So I had solved with below codes.
ActionBar actionBar = getActivity().getActionBar();
actionBar.setTitle(title);
// for refreshing UI
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
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
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:
public abstract class BaseListAdapter<BEAN extends Unique, VH extends BaseListViewHolder<BEAN>> extends BaseAdapter {
// data
private List<BEAN> mList;
// view holder
private List<VH> mViewHolderList = new ArrayList<>();
protected abstract void onBindViewHolder(VH holder, int position);
protected abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
VH viewHolder;
if (convertView == null) {
viewHolder = onCreateViewHolder(parent, getItemViewType(position));
convertView = viewHolder.itemView;
convertView.setTag(viewHolder);
mViewHolderList.add(viewHolder);
} else {
//noinspection unchecked
viewHolder = (VH) convertView.getTag();
}
viewHolder.setData(getItem(position));
viewHolder.setPosition(position);
onBindViewHolder(viewHolder, position);
return convertView;
}
@Override
public void notifyDataSetChanged() {
if (mViewHolderList.size() == 0) {
super.notifyDataSetChanged();
} else {
for (VH vh : mViewHolderList) {
onBindViewHolder(vh, vh.getPosition());
}
}
}
}
public class BaseListViewHolder<T> {
public View itemView;
private T data;
private int position;
public BaseListViewHolder(View itemView) {
this.itemView = itemView;
}
@Override
public T getData() {
return data;
}
@Override
public void setData(T data) {
this.data = data;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
Sorry for my bad english.