7

I have an Activity whose title keeps change, but sometimes its long and get ellipses by end. Can I set ellipse to middle ?

xmen
  • 1,947
  • 2
  • 25
  • 47

4 Answers4

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
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

Community
  • 1
  • 1
xmen
  • 1,947
  • 2
  • 25
  • 47
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
-3

Try using android:ellipsize

Refer android:ellipsize in the Android Developers page.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
cutebug
  • 475
  • 4
  • 10