iGio90 iGio90 - 2 months ago 10
Android Question

Get ActionBar TextView (title) in android

as per title i need to retrieve the textview that rapresent the activity title located in the ActionBar. I'm able to get the ActionBar view with the following method:

private View getActionBarView() {
View v = mWindow.getDecorView();
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
return v.findViewById(resId);
}


Does anyone know the id of the TextView so i can findViewById inside the actionbar view?

Solved:
well, i solved with the same method but by changing the id:

private TextView getActionBarTitle() {
View v = mWindow.getDecorView();
int resId = getResources().getIdentifier("action_bar_title", "id", "android");
return v.findViewById(resId);
}

Answer

All of the built-in Android layouts can be found in the SDK under:

.../android-sdk/platforms/android-<API-VERSION>/data/res/layout/

I think the one you are looking for in this case is action_bar_title_item.xml.
This is v19's:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical|start"
              android:orientation="vertical"
              android:paddingEnd="8dp">
    <TextView android:id="@+id/action_bar_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:singleLine="true"
              android:ellipsize="end" />
    <TextView android:id="@+id/action_bar_subtitle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="@dimen/action_bar_subtitle_top_margin"
              android:singleLine="true"
              android:ellipsize="end"
              android:visibility="gone" />
</LinearLayout>

Layouts can and do differ between API versions. In general the IDs will match but I can't say this would be true for every single layout and will always be so it's best to quickly look at each for differences if you are planning to use specific built in resources like this in case.

To do that, you will have to grab individual copies of the source code to look at each API version.
This can either be done though:

  • Your IDE: use Android's ADT plugin for your IDE of choice (Eclipse or AndroidStudio)
  • Manual method: calling the SDK's version yourself which is located within the SDK at:

    .../android-sdk/tools/android

You obviously need only grab the versions you are supporting and as I mentioned above each will have its own folder named after its single int version number. Eg:

.../android-sdk/platforms/android-19/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-15/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-11/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-10/data/res/layout/???

Uh-oh! No layout for API-10. If you want to use the action_bar_title_item.xml layout in that version of the API you can include and use Sherlock ActionBar or perhaps copy or modify the v11 layout to:

.../YourApp/res/layout-v10/action_bar_title_item.xml               (Eclipse)
.../YourApp/app/src/main/res/layout-v10/action_bar_title_item.xml  (Android Studio)

Your call. The same can be done it you just want to customise the layout. Copy the android built in version to your own layout folders and tweak to your heart's content. Just be mindful of what folders you place the files in so that they are overriding the desired, correct versions.