3

Trying to change the appearance of the AlertDialog, I subclassed DialogFragment with ThemedDialogFragment and I called this lines onStart():

int alertTitleId = getResources().getIdentifier("alertTitle", "id", "android");
TextView alertTitle = (TextView) dialog.findViewById(alertTitleId);
alertTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen._20ssp));

That resulted in a NullPointerException, even though I am sure there is a view with id alertTitle in the window. The HierarchyViewer(*) tool confirms.

Tried to do minor variants

e.g.

int alertTitleId = getResources().getIdentifier("android:id/alertTitle", null, null);

or

TextView alertTitle = (TextView) dialog.getWindow().findViewById(alertTitleId);

or

TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);

but none of this works.

Any idea, guys?

(*): HierarchyViewer shows the id I'm looking for exists HierarchyViewer shows the id I'm looking for exists

P.S.: I solved the AlertDialog styling problem, by operating on the themes.xml, I just want to know why this method didn't work (it should IMO). Having said so, I will downvote every answer that tells me how to style an AlertDialog because it's off-topic here. Thanks.


P.P.S: As a sidenote, I tried this method compiling agains the sdk level 22 and tested it on an Android emulator running Android Jelly Bean 4.3.1.


P.P.P.S: No luck on an emulator running 5.1.1 either.

doplumi
  • 2,938
  • 4
  • 29
  • 45
  • I have seen this problem happen before. likely, the id "alertTitle" is used in some levels of Android, but not all. Is it possible the hierarchy viewer is showing you values for level 22, but a different id is used in 4.3.1? – AdamMc331 May 05 '15 at 17:23
  • @McAdam331 I don't think so, I run the hierarchy viewer on the emulator with 4.3.1, and I was able to see that id. Anyway, I'm starting the 5.1.1 emulator atm... Will report back in a minute ;) – doplumi May 05 '15 at 17:27
  • I've had a somewhat similar problem: a dialog with a custom content view had a title and divider elements in pre-Lollipop devices, and didn't have any on the rest ones. Rather unexpected behaviour, all confirmed through hierarchy viewer. Had to check the api version to fight them. – AndroidEx May 05 '15 at 17:39
  • @McAdam331 No luck on 5.1.1 – doplumi May 05 '15 at 17:42
  • Hm. I really suspect it's that the id exists in some levels of android, but not others. In my app, I have to do a special check for build versions after lollipop to style the date picker, because I was having the same issue you are. – AdamMc331 May 05 '15 at 17:44
  • @Android777 Yes, but with the new AppCompat release, the material dialogs got ported back to pre-Lollipop devices. Before the release of the support library 22.1, the two dialogs were completely different: as you mention they had a divider before 5.0. So if you reference a divider, it's logic that you should check for the api version. Here we have two exact views with the same id though... – doplumi May 05 '15 at 17:49
  • @McAdam331 It should work on one API level at least if it was so. – doplumi May 05 '15 at 17:51

1 Answers1

0

It appears that you're trying to access an internal resource ID. In this case the package isn't "android" but "com.android.internal", but I'm not sure if even that would actually return the ID for you.

Android Dialog with modifiable single line title

Unfortunately, I cannot access their R.id.alertTitle, because it is part of com.android.internal.R.

Community
  • 1
  • 1
Coeffect
  • 8,772
  • 2
  • 27
  • 42