0

I have been using the following workaround to center the activity label (without having to resort to a custom title in XML layout):

((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);

This works great but every once in a while I see one of the users (on Google Play) getting a

java.lang.ClassCastException: com.android.internal.widget.ActionBarView cannot be cast to android.widget.TextView

These are probably tablet users who are running Android 3.x or higher.

Short of implementing my own custom title that would give me direct access to the activity label, can you recommend another way to avoid that ActionBarView to TextView ClassCastException?

Perhaps check for the Android version under which the app currently runs and go though another level of getChildAt(0)?

Bill The Ape
  • 3,261
  • 25
  • 44

2 Answers2

1

The action bar is defined in the theme. So if you want to avoid that ever showing up you need to pick different theme.

Alternatively you can detect what version of OS the user is using and in the case of being 11 (Honeycomb) and higher simply skip the action bar and get the next view.

Which would look something like

if (Integer.valueOf(android.os.Build.VERSION.SDK) > 11)
{
    ((TextView)((FrameLayout)((LinearLayout)((ViewGroup).getWindow().getDecorView()).getChildAt(1)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
}
else
   ((TextView)((FrameLayout)((LinearLayout)((ViewGroup).getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);

I would also like to add like Shark said, this is a total hack and is not very robust code. You should be using findViewById() to locate your views and avoid all of that super ugly casting. You can expect your hack or even the code I just put in to break in the future if you continue down that path.

Roloc
  • 1,910
  • 2
  • 13
  • 16
1
((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)

Start collecting child views from here, and search for your own TextView... You just need to skip the actionbar, no need to be nice about it when you started off with a hack anyway.

Shark
  • 6,513
  • 3
  • 28
  • 50
  • Yeah it kind of pained me just pasting that in there, but who am I to judge :) – Roloc Aug 16 '12 at 16:10
  • 1
    it's a terrible terrible workaround but hey, if it works... :) Should I even ask why findViewByID(R.whatever) doesn't work? :) or maybe attach a Tag to your particular textview and run thru all the views until you get it. – Shark Aug 16 '12 at 16:12
  • 1
    @Roloc LOL I got the hint. I have no idea why I am so afraid of a custom title... +1 to you both. – Bill The Ape Aug 16 '12 at 16:13
  • 1
    Just bear in mind that 1) always use getApplicationContext() and 2) you shouldn't generally store view references in global vars as they leak but I am yet to see why this practice is supposedly bad. – Shark Aug 16 '12 at 16:20
  • @Shark OK now I recall why I was trying to get away from a custom title: It cannot be combined with certain features such as FEATURE_PROGRESS. – Bill The Ape Aug 16 '12 at 16:21
  • 1
    http://stackoverflow.com/questions/11254366/combine-custom-title-with-feature-progress this was the first hit when I googled "FEATURE_PROGRESS" actually. – Shark Aug 16 '12 at 16:23
  • @Shark Yup. I found that one through Google, too. But it has no answers. – Bill The Ape Aug 16 '12 at 16:28
  • It has very useful links however, with questions simialar to yours with accepted answers... try digging thru those. My work(shift) here is done :) – Shark Aug 16 '12 at 16:31
  • @Shark Actually I don't need a progress bar in my app, so I may as well take your recommendation. I will try to follow [this 4.x sample code](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.0.1_r1/com/example/android/apis/app/CustomTitle.java) or [this 2.2 one](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/2.2_r1.1/com/example/android/apis/app/CustomTitle.java). – Bill The Ape Aug 16 '12 at 16:36
  • 1
    Glad to have helped out :) Take care. – Shark Aug 16 '12 at 16:37