Amir Emad
Amir Emad

Reputation: 81

Change Text size according to screen programatically

Hello I would like to ask

Now I have lots of textviews adjusted and styled correctly the problem is when screen size changes the textviews either become too big or too small

I have tried making different layout files but It does not work though

I used SP and still text overlaps the screen when switching from a device to another

I have made over than 10 layout files for different screens and still I cannot control over it

The question is :

Is there a way to change text size logically instead of having to create alternative layouts or dimens files ??

Here is an example of my code

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="org.ultradroid.metaldetectorfree.welcome1"
    android:orientation="vertical"
    android:weightSum="1"
    tools:ignore="UseCompoundDrawables">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="UselessParent">

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="match_parent"
            android:layout_height="260dp"
            app:srcCompat="@drawable/metal"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/welcome"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            android:textColor="@color/aqua"
            android:textSize="60sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/WelcomeTextDescription"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/aqua"
            android:textSize="40sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:orientation="horizontal">

            <android.support.v7.widget.AppCompatButton
                android:id="@+id/next"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/next"
                android:textAllCaps="false"
                android:textAppearance="@style/TextAppearance.AppCompat.Button"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:textStyle="bold"
                app:backgroundTint="@color/aqua" />

        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Views: 6082

Answers (3)

Vamsi Smart
Vamsi Smart

Reputation: 928

You can use autosizing for TextView

<TextView
  android:layout_width="match_parent"
  android:layout_height="200dp"
  app:autoSizeTextType="uniform" />

This will automatically adjust the text size according to your screen width

for more details read android official documentation

Upvotes: 2

Amir Emad
Amir Emad

Reputation: 81

Hello guys I found the answer

This awesome library will help everyone do it https://github.com/intuit/sdp

No need for many dimens or many layouts you just need this

Upvotes: 3

Abdul Kawee
Abdul Kawee

Reputation: 2727

What you are doing wrong is you are defining a static text size for all the text views use dimens.xml for different screen sizes and assign the values of the dimen to the textViews like in the below image

I would suggest you to have a look at Dimenfy plugin and also at this link.

enter image description here

Upvotes: 1

Related Questions