The below code gives Resources$NotFoundException
TypedValue value = new TypedValue();
((Activity)context).getResources().getValue(android.R.attr.listPreferredItemHeight, value, true);
EDIT: More code added in response to answer.
When I run the below code, all members of displayMetrics are 0. As is ret.
TypedValue value = new TypedValue();
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity)context).getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
float ret = value.getDimension(displayMetrics);
This works:
EDIT: You get zero because haven’t initialized the
DisplayMetricsinstance properly. It needs a frame of reference (a display) to do any meaningful conversion.On my Nexus 1
sis 64.0dip andretis 96.