Search code examples
androidandroid-layoutfont-sizeandroid-fonts

Can not handle font size in all screen size


I am building an Android application that will work on all screen sizes, I am using sp for the text size but there is a problem with the font size on different android screens.

The attached photo for a tablet 10 inch, can anyone help me?

screenshot:

screenshot

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

// In this TesxtView i used sp instead of dp.

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/_6sdp"
        android:layout_marginEnd="@dimen/_6sdp"
        android:layout_marginLeft="@dimen/_6sdp"
        android:layout_marginRight="@dimen/_6sdp"
        android:layout_marginStart="@dimen/_6sdp"
        android:layout_marginTop="@dimen/_110sdp"
        android:text="Username"
        android:textAlignment="center"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="@+id/rectangle_login"
        app:layout_constraintLeft_toLeftOf="@+id/rectangle_login"
        app:layout_constraintRight_toRightOf="@+id/rectangle_login"
        app:layout_constraintStart_toStartOf="@+id/rectangle_login"
        app:layout_constraintTop_toTopOf="@+id/rectangle_login" />


</android.support.constraint.ConstraintLayout>

Solution

  • Use this library for set text size in sp https://github.com/intuit/ssp

    <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/action_cancel"
                android:textColor="@color/purple_light"
                android:textSize="@dimen/_12ssp"
                app:font="@{`hnc_roman`}"
                app:layout_constraintEnd_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:id="@+id/text_view_cancel"
                android:onClick="@{v -> viewModel.onCancelClick(v)}"
                tools:ignore="MissingConstraints" />