Question

is it possible to the add a compound drawable (something like drawableLeft) to the standard action bar title without using a custom view?

Atm i am using a SpannableString for the title (in order to use a custom typeface). If I can't set a compund drawable would it be possible to use an ImageSpan that i could append to my SpannableString?

Thx

Was it helpful?

Solution

Yep, absolutely. Here's a quick example:

    final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
    final TextView abTitle = (TextView) findViewById(abTitleId);
    abTitle.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_icon, 0, 0, 0);

OTHER TIPS

Thanks for adneal's answer, but it doesn't work with android lollipop and I found an solution according to his method. It's lack of standardization but works well.

ViewGroup actionViewGroup = (ViewGroup) findViewById(getResources().getIdentifier("action_bar", "id", "android"));
if (actionViewGroup != null && actionViewGroup.getChildCount() > 0) {
    View view = actionViewGroup.getChildAt(0);
    if (view instanceof TextView) {
        TextView tv = (TextView) view;
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_test, 0, 0, 0);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top