Rose Perrone
Rose Perrone

Reputation: 63546

How can I get an ActionBar's TextView?

I can get an ActionBar's view using getCustomView. Is there a way to get its TextView from that?

Upvotes: 0

Views: 148

Answers (2)

Eng.Fouad
Eng.Fouad

Reputation: 117587

Try recursively? (might not be a good solution though!)

static List<TextView> textViews = new ArrayList<TextView>();

public static <T> void searchRecursively(View parent, Class<T> clazz)
{
    if(clazz.isInstance(parent)) textViews.add(clazz.cast(parent));
    if(parent instanceof ViewGroup)
    {
        ViewGroup vg = (ViewGroup) parent;
        int count = vg.getChildCount();
        for(int i = 0; i < count; i++)
        {
            View v = vg.getChildAt(i);
            searchRecursively(v);
        }
    }
}

Use it like:

searchRecursively(theView, TextView.class);

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

Not in any way that is going to be reliable across OS versions and devices.

If you are attempting to set the title of the action bar, please use setTitle() on ActionBar. If you are trying to style the title, you should be able to do that via a theme.

Or, hide the title and render your own via setCustomView().

Upvotes: 2

Related Questions