Why getResources () doesn't work. GetIdentifier () works here (API level 22)?
Trying to change the appearance of the AlertDialog, I subclassed DialogFragment
with ThemedDialogFragment
and I called these 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));
This resulted in NullPointerException
, though I'm pretty sure the window has a view with id alertTitle
. The HierarchyViewer (*) tool confirms.
Tried doing minor options
eg.
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 am looking for exists
PS: I solved the styling problem by AlertDialog
working on themes.xml
, I just want to know why this method didn't work (it should IMO). Having said that, I will go over each answer that tells me how to create an AlertDialog, because that is off topic here. Thank.
PPS: As a side piece, I tried this compilation method again at sdk 22 level and tested it on an Android emulator running Android Jelly Bean 4.3.1.
PPPS: No luck with an emulator running 5.1.1.
source to share
It seems that you are trying to access the internal resource id. In this case, the package is not "android" but "com.android.internal", but I'm not sure if even that will actually return the ID for you.
Android dialog mutable single line type
Unfortunately, I cannot access their R.id.alertTitle because it is part of com.android.internal.R.
source to share