I have an Activity whose title keeps change, but sometimes its long and get ellipses by end. Can I set ellipse to middle ?
Asked
Active
Viewed 4,586 times
4 Answers
20
You can do this:
final int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
final TextView title = (TextView)getWindow().findViewById(actionBarTitle);
if ( title != null ) {
title.setEllipsize(TextUtils.TruncateAt.MIDDLE);
}

Diego Torres Milano
- 65,697
- 9
- 111
- 134
-
1I don't see the 1.6 in your question. It should be the same, but different id, try to find it using hierarchyviewer – Diego Torres Milano May 28 '12 at 06:02
-
3This too is a hack. It's likely to break. At least check for nulls and catch exceptions. – pjv Jan 10 '13 at 15:26
-
@xmenW.K."android.R.id.title" in a non-actionbar version. – wangqi060934 Sep 09 '13 at 07:12
1
((TextView) ((FrameLayout) ((LinearLayout) ((ViewGroup) getWindow()
.getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0))
.setEllipsize(TextUtils.TruncateAt.MIDDLE);
got it from android:set title bar in middle
0
if you user the Toolbar you can add this function before settitle
private void setToolbarTextEllipsizeInMiddle(Toolbar toolbar){
try {
toolbar.setTitle("'");
for (int i = 0; i < toolbar.getChildCount(); i++) {
View view = toolbar.getChildAt(i);
if (view instanceof TextView) {
TextView tv = (TextView) view;
tv.setEllipsize(TextUtils.TruncateAt.MIDDLE);
}
}//end for i
toolbar.setTitle("");
} catch (Exception e) {
}
}

geass code
- 316
- 3
- 4