iGio90
iGio90

Reputation: 3291

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);
}

Upvotes: 3

Views: 8793

Answers (3)

user25
user25

Reputation: 3195

2016, for other developers who got to this question

here the answer: https://stackoverflow.com/a/32614598/4548520

using this tool https://developer.android.com/studio/profile/hierarchy-viewer-walkthru.html we can find any child of toolbar (actionbar)

enter image description here

From screenshot:

  • AppCompatTextView (id 0) - title
  • AppCompatTextView (id 2) - subtitle

both doesn't include id like action_bar_title anymore

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

Upvotes: 2

headsvk
headsvk

Reputation: 2796

If you're using support Toolbar instead of ActionBar, here's a way how to get the title TextView.

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// loop through all toolbar children right after setting support 
// action bar because the text view has no id assigned

// also make sure that the activity has some title here
// because calling setText() with an empty string actually
// removes the text view from the toolbar

TextView toolbarTitle = null;
for (int i = 0; i < toolbar.getChildCount(); ++i) {
    View child = toolbar.getChildAt(i);

    // assuming that the title is the first instance of TextView
    // you can also check if the title string matches
    if (child instanceof TextView) {
        toolbarTitle = (TextView)child;
        break;
    }
}

Upvotes: 0

indivisible
indivisible

Reputation: 5012

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.

Upvotes: 7

Related Questions