Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reference ActionBar title View on Lollipop

I have an Activity which extends the AppCompatActivity and uses the support ActionBar. Compile SDK version is 23, test devices SDK are 21-22. I need to get the ActionBar's title TextView, however, the most known way of doing this

int resId = getResources().getIdentifier("action_bar_title", "id", "android");
return (TextView) findViewById(resId);

doesn't work starting with API 21. Is there a way to reference the title TextView on Lollipop?

like image 536
Droidman Avatar asked Feb 09 '23 01:02

Droidman


1 Answers

With AppCompatActivity, You can retrieve the TextView that you want indirectly, because it seems that this TextView has no id, like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
TextView textView = (TextView) toolbar.getChildAt(0);

I used the Hierarchy Viewer tool to get the view tree. (Tool that you can find into Android Device Monitor under Hierarchy View perspective)

Action bar view tree

like image 140
xiaomi Avatar answered Feb 11 '23 16:02

xiaomi