HelloWorld
HelloWorld

Reputation: 47

TextView on different displays


Help to understand please. I need to show up in a specific box. I do it like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fon"
    tools:context=".MainActivity" >

....

<TextView
        android:id="@+id/textView3"
        android:layout_width="200dp"
        android:layout_height="210dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:includeFontPadding="false"
        android:lineSpacingMultiplier="0.8"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp" />

....
 </RelativeLayout>

Everything works at four inch screen well but if take a big diagonal, it will be bad.
At increase in the screen the textView size doesn't change.
I use "dp" and "sp", instead of static "px", but does not work... Why Is this happening ?

screenshots:

4 inch

5.4 inch

Upvotes: 0

Views: 149

Answers (3)

Ivan Skoric
Ivan Skoric

Reputation: 1210

You can create different resource directories for different screen sizes and densities and then create dimens.xml in each of them providing text size to be used on specific screen size, for example:

res/values-sw420dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="font_size">26sp</dimen>
</resources>

res/values-sw600dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="font_size">30sp</dimen>
</resources>

res/values-sw720dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="font_size">36sp</dimen>
</resources>

And then call it in your layout file:

android:textSize="@dimen/font_size"

This is a solution for the text size, but you can do the same thing for width and height.

More resources:

Upvotes: 1

Fate Chang
Fate Chang

Reputation: 207

You should adjust the TextView android:layout_width and android:layout_height to

android:layout_width="wrap_content"
android:layout_height="wrap_content"

If you want the TextView to center inside the white background, you should add another attribute

android:layout_centerInParent="true"

Upvotes: 0

Pihu
Pihu

Reputation: 1039

The text view size is static because you have defined a fixed size to them.Use wrap content.

  android:layout_width="wrap_content"
   android:layout_height="wrap_content"

Upvotes: 0

Related Questions