2

Is there any maximum text size for TextView in Android?

My application has to display some text in around 290sp. However, I found that some of the devices cannot display it and a whole blank page is TextView is shown.

In Samsung Galaxy Note 5 (API 23), it works find. In Google Nexus 5 (API 19), the maximum text size is 239sp. In Samsung Galaxy S7 (API 23), the maximum text size is 179sp.

Below is my demo code: Layout XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xxx.texttutorial.MainActivity">
    <Button
        android:id="@+id/buttonPlus"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="+10"/>
    <TextView
        android:id="@+id/textViewCurrentTextSize"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/buttonPlus"
        android:text="130"
        android:gravity="center_horizontal"
        android:textSize="40sp"
        />
    <Button
        android:id="@+id/buttonMinus"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textViewCurrentTextSize"
        android:text="-10"
        />
    <TextView
        android:id="@+id/currentText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textViewCurrentTextSize"
        android:textSize="130sp"
        android:gravity="center"
        android:text="X"/>
</RelativeLayout>

MainActivity JAVA

public class MainActivity extends AppCompatActivity {

  int textSize = 130;

  TextView currentText, textViewCurrentTextSize;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    currentText = (TextView)findViewById(R.id.currentText);
    textViewCurrentTextSize = (TextView)findViewById(R.id.textViewCurrentTextSize);
    (findViewById(R.id.buttonPlus)).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        textSize += 10;
        currentText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        textViewCurrentTextSize.setText(textSize+"");
      }
    });
    (findViewById(R.id.buttonMinus)).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        textSize -= 10;
        currentText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        textViewCurrentTextSize.setText(textSize+"enter code here");
      }
    });
  }
}
Myrick Chow
  • 332
  • 1
  • 6
  • 16
  • 1
    Have newer tried such a large text. And don't know if there is any limit, but know possible solution. You might don't have to use TextView, but draw text on the view.s canvas directly. That way you should not suffer from any unexpected limits – Vladyslav Matviienko Jan 26 '17 at 06:43
  • 1
    Possible duplicate of [Font size too large to fit in cache](https://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache) – Stefan Haustein Nov 19 '17 at 23:12

1 Answers1

1

Its because of Screen Density Pixel, try to assign different text sizes for different screen resolution in value resource files. Please read more

chiru
  • 635
  • 1
  • 7
  • 14