Myrick Chow
Myrick Chow

Reputation: 342

Android textView bug? Maximum text size in sp

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");
      }
    });
  }
}

Upvotes: 2

Views: 1924

Answers (1)

chiru
chiru

Reputation: 645

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

Upvotes: 1

Related Questions