Grabbing a pointer to a TextView of a tab on the action bar?

Some other users and I are developing an Android application for the Stack Exchange chat network. We're adding a tutorial for each activity to explain the UI to the user. However, we've run into a bit of a road block.

The tutorial library wants a pointer to a view (be it a TextView, ImageView, whatever) in order to get the coordinates of the view in the display so it knows where to draw the drop shadows and stuff.

We have one activity which uses the standard "Tabbed Activity" from Android Studio, so we aren't using any custom toolbars.

The action bar looks like this:

enter image description here

And we want to grab a pointer to the TextView on each tab that holds the title of the tab.

So for example, we want to be able to access this Textview:

enter image description here

We haven't been real successful in finding anything on the internet about how to do this. It appears to be relatively easy if you're using a custom toolbar, but we aren't.

Digging in the AOSP source code, we found a potential way to do it, but the fields that we needed access to were either private or otherwise unaccessible from the main activity code.

So the question is, how can we grab a pointer to that TextView? Is it even possible?

Upvotes: 4

Views: 129

Answers (1)

Well, it isn't pretty but we found a way to do it. Using the layout inspector in Android Device Monitor to look at the view hierarchy, we were able to grab a pointer to it in the following way.

Keep in mind:

  • You may need to adjust for your activity's layout

  • If you're using a custom toolbar there's an easier way to do this

That being said, here's what worked for this specific use case:

ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
LinearLayout testb = (LinearLayout) viewGroup.getChildAt(0);
FrameLayout testc = (FrameLayout) testb.getChildAt(1);
ActionBarOverlayLayout testd = (ActionBarOverlayLayout) testc.getChildAt(0);
ActionBarContainer teste = (ActionBarContainer) testd.getChildAt(1);

LinearLayoutCompat testg;

if (getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT)
{
    ScrollingTabContainerView testf = (ScrollingTabContainerView) teste.getChildAt(2);
    testg = (LinearLayoutCompat) testf.getChildAt(0);
}
else //Landscape
{
    Toolbar teste2 = (Toolbar) teste.getChildAt(0);
    ScrollingTabContainerView testf = (ScrollingTabContainerView) teste2.getChildAt(0);
    testg = (LinearLayoutCompat) testf.getChildAt(0);
}

testg.setId(android.R.id.tabcontent);

//String IdAsString = testg.getResources().getResourceName(testg.getId());
//Log.e("TestG", IdAsString);

TutorialStuff.chatsExplorationTutorial(this, testg);

And here's the end result:

enter image description here

Upvotes: 6

Related Questions