Thanks @John Leehey and @PeterH:
desiredSp = getResources().getDimension(R.dimen.desired_sp);
density = getResources().getDisplayMetrics().density;
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, desiredSp / density);
The thing is if you define R.dimen.desired_sp to 25 in your dimen.xml