java - How can I tell if my actionbar's title is going to be truncated? -


in app want display 2 pieces of information in title of actionbar. since title can of various length, devices not able display both pieces of information. if happens can display second string using getsupportactionbar().setsubtitle.

however, issue i'm having don't know how check if title going truncated. i've tried using getsupportactionbar().istitletruncated(), returns false.

        if (getsupportactionbar().istitletruncated())         {             getsupportactionbar().settitle(username);             getsupportactionbar().setsubtitle("❤" + " " + string.valueof(rating));         }         else         {             getsupportactionbar().settitle(username + "  " + "❤" + " " + string.valueof(rating));         } 

according https://code.google.com/p/android/issues/detail?id=81987 istitletruncated() requires layout pass in order return. sadly don't think istitletruncated() work me.

does know how else can achieved?

update: have been playing around displaymetrics , may have found possible, albeit inconsistent solution.

i can width of device in pixels by:

displaymetrics displaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); int width = displaymetrics.widthpixels; 

and can width of title string by:

paint paint = new paint(); float scaledpx = 18 * displaymetrics.density; paint.settextsize(scaledpx); float stringsize = paint.measuretext(username + "  " + "❤" + " " + string.valueof(rating)); 

and if divide string width device width, can find out percentage of screen width string occupies. bit of trial , error can work out , percentage title truncates.

i've far tried on 3 devices , unfortunately number isn't consistent (truncation has been occurring 30% 45% app).

i not happy solution , feel there must more reliable methods of working out.

i having same issue , need consistent library support problem.

here did:

activity.xml:

<android.support.design.widget.appbarlayout> <android.support.v7.widget.toolbar> //other elements <textview   android:id="@+id/detail_title"   android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:alpha="1.0" android:gravity="start|center_horizontal|left" android:text="title"   android:textappearance="@style/base.textappearance.appcompat.widget.actionbar.title" // +++++++ important +++++++++++++ // these 2 lines important.  android:maxlines="2" // title text expand 2 lines. android:linespacingmultiplier="0.9" // line spacing factor.                     />  />  /> 

and

activity.java:

toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar);  // using textview in toolbar in activity_main.xml // disable default title getsupportactionbar().setdisplayshowtitleenabled(false); // set title using detail_text_view detail_text_view = (textview) findviewbyid(r.id.detail_title); detail_text_view.settext("a b c d e f g h j k l m n o p q r s t u v w x y z"); 

result:
enter image description here

now, if want change actionbar height make more linespace, can try reflection mcontentheight property of actionbar. , set accordingly. didn't experiment should give try.

currently, trying multiline title bar in collapsingtoolbar using reflection well.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -