Created
September 29, 2014 18:28
-
-
Save gadmyth/a12baab57e0f2f2e332a to your computer and use it in GitHub Desktop.
android set text size in code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
Log.i("TestActivity", "onCreate"); | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_test); | |
TextView view = (TextView) findViewById(R.id.test_size); | |
int size = getResources().getDimensionPixelSize(R.dimen.test_text_size); | |
size = px2sp(getApplicationContext(), size); | |
view.setTextSize(size); | |
} | |
public static int px2sp(Context context, float pxValue) { | |
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; | |
return (int) (pxValue / fontScale + 0.5f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment