Skip to content

Instantly share code, notes, and snippets.

@Keshava11
Last active August 29, 2015 14:01
Show Gist options
  • Save Keshava11/9d3981e017d683ed1a74 to your computer and use it in GitHub Desktop.
Save Keshava11/9d3981e017d683ed1a74 to your computer and use it in GitHub Desktop.
Access ActionBar Title TextView For All Android, Setting Marquee To It And Also Detecting If Marquee Is Activated And Hence Shrinking(Customizing) TextView
This is about how to access action bar title for all versions, detecting if marquee is activated for the actionbar title and if it is then customizing the font.
This mainly emphasizing on avoiding custom view as long as one can access actionbar title textview this way.
Since there is no direct way to customize the title TextView in android, a way is :
getResources().getIdentifier(String resId, String resType, String package);
So when it comes for actionbar in all versions of android we can access it as :
int titleId = 0;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
{
titleId = getResources().getIdentifier("action_bar_title", "id", "android");
}
else
{
titleId = R.id.action_bar_title;
}
And following is the code for detecting if marquee is activated or not and then changing the font style and shrinking it if marquee gets activated
/**
* This method adds the marquee effect to the textview in case its long enough to fit with in the screen
* @param iTitle string defining content to set as title
*/
private void createCustomTitleTxv(String iTitle)
{
// Spannable for customizing font
SpannableString spannable = new SpannableString(iTitle);
spannable.setSpan(new TypefaceSpan("Helvetica"), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int titleId = 0;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
{
titleId = getResources().getIdentifier("action_bar_title", "id", "android");
}
else
{
titleId = R.id.action_bar_title;
}
// titleTxv : member reference to the action bar title textview
// Setting Marquee here
titleTxv = (TextView) findViewById(titleId);
titleTxv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
titleTxv.setMarqueeRepeatLimit(-1);
titleTxv.setFocusable(true);
titleTxv.setFocusableInTouchMode(true);
titleTxv.setHorizontallyScrolling(true);
titleTxv.setFreezesText(true);
titleTxv.setSingleLine(true);
titleTxv.setText(spannable);
// Adding listener for listening changes in layout of textview
titleTxv.getViewTreeObserver().addOnGlobalLayoutListener(mMarqueeListener);
titleTxv.post(new Runnable() {
@Override
public void run() {
// Final callback to check if marquee was activated or do the Shrinking text size
if(mMarqueeActivated) {
float shrinkSize = getResources().getDimension(R.dimen.shrink_exc_name_size);
titleTxv.setTextSize(shrinkSize);
}
}
});
}
/**
* This listener (for listening layout changes in view tree) here will be used to detect if marquee is activated and hence allows to shrink the size of title TextView and customize it
*/
private ViewTreeObserver.OnGlobalLayoutListener mMarqueeListener = new ViewTreeObserver.OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
if(titleTxv!=null)
{
// Reference to the layout on which text is drawn
Layout titleLayout = titleTxv.getLayout();
if ( titleLayout != null){
if(titleTxv.getWidth()<(int)titleLayout.getLineWidth(0))
{
// Marquee Effect. So Shrinked Customized TextView
mMarqueeActivated = true;
// Remove listener
titleTxv.getViewTreeObserver().removeGlobalOnLayoutListener(this)
}
}
}
}
};
"A thought, even a possibility, can shatter and transform us."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment