Skip to content

Instantly share code, notes, and snippets.

@namchuai
Created October 9, 2018 02:21
Show Gist options
  • Select an option

  • Save namchuai/92adbad54ecf2b638d126d44681f20f9 to your computer and use it in GitHub Desktop.

Select an option

Save namchuai/92adbad54ecf2b638d126d44681f20f9 to your computer and use it in GitHub Desktop.
public static int getApproxXToCenterText(String text, Typeface typeface, int fontSize, int widthToFitStringInto) {
Paint p = new Paint();
p.setTypeface(typeface);
p.setTextSize(fontSize);
float textWidth = p.measureText(text);
int xOffset = (int)((widthToFitStringInto-textWidth)/2f) - (int)(fontSize/2f);
return xOffset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment