13

What should be used instead of:

StaticLayout layout = new StaticLayout(text, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false);

Gives this warning:

warning: [deprecation] StaticLayout(CharSequence,TextPaint,int,Alignment,float,float,boolean) in StaticLayout has been deprecated StaticLayout layout = new StaticLayout(text, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false);

MiguelSlv
  • 14,067
  • 15
  • 102
  • 169
  • 2
    I highly recommend downloading the sources of whichever Android API you are targeting, which will also include javadocs. For example, the constructor you are using has the following javadoc: /** * @deprecated Use {@link Builder} instead. */ – Ovidiu Nov 01 '18 at 16:55

1 Answers1

16

Use StaticLayout.Builder. Go through here for more details: https://developer.android.com/reference/android/text/StaticLayout.Builder

for your case use:

StaticLayout.Builder sb = StaticLayout.Builder.obtain(text, 0, text.length(), paint, width)
                          .setAlignment(Layout.Alignment.ALIGN_NORMAL)
                          .setLineSpacing(mSpacingAdd, mSpacingMult)
                          .setIncludePad (false);
StaticLayout layout = sb.build();
Adeel Ahmad
  • 939
  • 1
  • 10
  • 20
S.M
  • 601
  • 7
  • 17
  • 2
    Unfortunate, StaticLayout.Builder requires API 23 or above. My project is compatible with API 21. – MiguelSlv Nov 01 '18 at 17:45
  • 7
    You can use this if you want:- `// Check if we're running on Android 6.0 or higher if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { StaticLayout.Builder sb= StaticLayout.Builder.obtain(text, paint, width) .setAlignment(Layout.Alignment.ALIGN_NORMAL) .setLineSpacing(mSpacingAdd, mSpacingMult) .setIncludePad (false); StaticLayout layout = sb.build(); } else { StaticLayout layout = new StaticLayout(text, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); }` – S.M Nov 02 '18 at 14:59