Android override: ellipsis on ActionBar subtitle

My activity uses the ActionBar.setSubtitle function to display the full location path of the current file system. This means that the substring near the end of the subtitle is more important than the beginning, so I thought that overriding the ActionBar style would ellipse the text at the beginning rather than the end, for example:

RES / value / themes.xml

 <style name="Theme.Holo" parent="android:Theme.Holo"> <!-- Action bar styles --> <item name="android:actionBarStyle">@style/Widget.Holo.ActionBar</item> </style> 

RES / value / styles.xml

 <style name="Widget.Holo.ActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:subtitleTextStyle">@style/TextAppearance.Holo.Widget.ActionBar.Subtitle</item> </style> <style name="TextAppearance.Holo.Widget.ActionBar.Subtitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle"> <item name="android:ellipsize">start</item> </style> 

When I install this on my ICS (Nexus S) device, the text is still (incorrectly) ellipsed at the end. If I change my version of TextAppearance.Holo.Widget.ActionBar.Subtitle to include an element for android:textSize , the subtitle text will be resized accordingly, so it seems to me that I look in the right style; I just can't understand why it does not respect the android:ellipsize .

Paste all this into the question format: how can I override, using styles / themes, the android:ellipsize property of the subtitle ActionBar in the ActionBar ?

+4
source share
1 answer

It seems to me that this is not possible with themes and styles, but I found a workaround for setting the ellipsis value at runtime.

The most difficult thing is to find the text (or subtitles) of the TextView, because the identifier of this view is in the internal android package.

So you need to create a stub class in your project

 package com.android.internal; public class R { public static final class id { public static int action_bar_title = 0; public static int action_bar_subtitle = 0; } } 

After that, you need to find this view in the code (I do it with the following code, it works with sherlock as well as with default files)

 public class ActionBarUtils { public static TextView getActionbarTitle(Activity activity) { TextView title = (TextView) activity.findViewById(R.id.abs__action_bar_title);// for ActionBarSerlock if (title == null) title = (TextView) activity.findViewById(com.android.internal.R.id.action_bar_title);// for default action bar return title; } } 

Now you can set the ellipsis value in the operation code:

 getActionBarTitle().setEllipsize(TextUtils.TruncateAt.MIDDLE) 

PS: Make sure you call getActionBarTitle when the header is visible (after calling setDisplayShowTitleEnabled (true)), otherwise you will get a NullPointerException.

PPS: If you are using obfuscation proguard, you should add to proguard.cfg:

 -keep class com.android.internal.R*{ *; } 
+10
source

Source: https://habr.com/ru/post/1391092/


All Articles