Automatically adjust the textview font size to adapt to the textview Length

Source: Internet
Author: User
  1. Package com. Test. Android. textview;
  2. Import Android. content. context;
  3. Import Android. Graphics. paint;
  4. Import Android. util. attributeset;
  5. Import Android. widget. textview;
  6. Public class customtextview extends textview {
  7. Private Static float default_min_text_size = 10;
  8. Private Static float default_max_text_size = 20;
  9. // Attributes
  10. Private paint testpaint;
  11. Private float mintextsize, maxtextsize;
  12. Public customtextview (context, attributeset attrs ){
  13. Super (context, attrs );
  14. Initialise ();
  15. }
  16. Private void initialise (){
  17. Testpaint = new paint ();
  18. Testpaint. Set (this. getpaint ());
  19. // Max size defaults to the intially specified text size unless it is
  20. // Too small
  21. Maxtextsize = This. gettextsize ();
  22. If (maxtextsize <= default_min_text_size ){
  23. Maxtextsize = default_max_text_size;
  24. }
  25. Mintextsize = default_min_text_size;
  26. };
  27. /**
  28. * Re size the font so the specified text fits in the text box * Assuming
  29. * The text box is the specified width.
  30. */
  31. Private void refittext (string text, int textwidth ){
  32. If (textwidth> 0 ){
  33. Int availablewidth = textwidth-This. getpaddingleft ()
  34. -This. getpaddingright ();
  35. Float trysize = maxtextsize;
  36. Testpaint. settextsize (trysize );
  37. While (trysize> mintextsize)
  38. & (Testpaint. measuretext (text)> availablewidth )){
  39. Trysize-= 1;
  40. If (trysize <= mintextsize ){
  41. Trysize = mintextsize;
  42. Break;
  43. }
  44. Testpaint. settextsize (trysize );
  45. }
  46. This. settextsize (trysize );
  47. }
  48. };
  49. @ Override
  50. Protected void ontextchanged (charsequence text, int start, int before,
  51. Int after ){
  52. Super. ontextchanged (text, start, before, after );
  53. Refittext (text. tostring (), this. getwidth ());
  54. }
  55. @ Override
  56. Protected void onsizechanged (int w, int H, int oldw, int oldh ){
  57. If (W! = Oldw ){
  58. Refittext (this. gettext (). tostring (), W );
  59. }
  60. }
  61. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.