Last active
March 2, 2017 04:02
-
-
Save SarvagyaVaish/b44b9f748c0516e77802558dc4987c4b to your computer and use it in GitHub Desktop.
Calculating proper leading
This file contains 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
TextView title_TextView = (TextView) findViewById(R.id.title); | |
Paint title_Paint = title_TextView.getPaint(); | |
Paint.FontMetrics title_FontMetrics = title_Paint.getFontMetrics(); | |
TextView subheading_TextView = (TextView) findViewById(R.id.subheading); | |
Paint subheading_Paint = subheading_TextView.getPaint(); | |
Paint.FontMetrics subheading_FontMetrics = subheading_Paint.getFontMetrics(); | |
Log.d("Survy", "title descent: " + title_FontMetrics.descent); | |
Log.d("Survy", "title ascent: " + title_FontMetrics.ascent); | |
Log.d("Survy", "subheading descent: " + subheading_FontMetrics.descent); | |
Log.d("Survy", "subheading ascent: " + subheading_FontMetrics.ascent); | |
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics(); | |
float dp = 40; | |
float px = dp * ((float) displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); | |
float spacing = px - title_FontMetrics.bottom + subheading_FontMetrics.top; | |
Log.d("Survy", "spacing: " + spacing); | |
View view = (View) findViewById(R.id.spacing); | |
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(spacing))); | |
float spacing_dp = spacing / ((float) displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); | |
Log.d("Survy", "spacing dp: " + spacing_dp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment