eliadesign
eliadesign

Reputation: 15

Change textSize in a TextView

In this code below from an android application on android studio, I have a LinearLayout that contains a TextView whose size I want to change for example say from 100 to 20. But the thing is, whenever I change the android:textSize="100.0dip" to any other number, the size still remains the same in the application when running it on my phone.

Code:

<ScrollView
    android:id="@id/qp_body_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/widget34"
    android:layout_below="@id/widget99"
    android:layout_marginBottom="5.0dip"
    android:layout_marginTop="10.0dip"
    android:clickable="true"
    android:focusable="true"
    android:foregroundGravity="right">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/qp_body_wrapper_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <TextView
            android:id="@id/qp_body"
            style="@style/QuoteViewText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/dashboard_qod_bg"
            android:clickable="true"
            android:focusable="true"
            android:gravity="right"
            android:padding="10.0dip"
            android:textAlignment="viewEnd"
            android:textColor="#FFFFFF"
            android:textSize="100.0dip"
            tools:ignore="RtlCompat,RtlHardcoded" />
    </LinearLayout>
</ScrollView>

and here is the QuoteViewText style :

<style name="QuoteViewText">
        <item name="android:textSize">100.0dip</item>
        <item name="android:typeface">serif</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#fffcfcfc</item>
        <item name="android:shadowColor">#ff102e46</item>
        <item name="android:shadowDx">0.0</item>
        <item name="android:shadowDy">1.0</item>
        <item name="android:shadowRadius">2.0</item>
    </style>

Upvotes: 1

Views: 10198

Answers (5)

Arjun
Arjun

Reputation: 1342

You are using dip in textsize,instead of that just use dp or sp.dp and sp both will work but normally in textview ,we use sp .

Upvotes: 1

Dimitar
Dimitar

Reputation: 4801

Tip: Use dimens.xml to store your concrete values for easier and cleaner code management.

Besides the change from dip to sp and the superfluous double setting of textSize, there is no problem with your TextView code and it should work fine. There some other small issues as possibly using @+id/qp_body instead and using match_parent instead of the deprecated fill_parent, but still it should be working.

This is shooting in the dark, but check, if you are not somehow overwriting the value programatically in code. If not, you could always try to and see what happens:

TextView mTextView = (TextView) findViewById(R.id.qp_body);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);

Or using a dimension resource:

TextView mTextView = (TextView) findViewById(R.id.qp_body);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.my_text_size));

Upvotes: 1

Sachin Rajput
Sachin Rajput

Reputation: 4344

You are using dip , instead you should sp for textSize.

you can update your code files with these below files , it will solve your problem

<ScrollView
    android:id="@id/qp_body_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/widget34"
    android:layout_below="@id/widget99"
    android:layout_marginBottom="5.0dip"
    android:layout_marginTop="10dp"
    android:clickable="true"
    android:focusable="true"
    android:foregroundGravity="right">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/qp_body_wrapper_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <TextView
            android:id="@id/qp_body"
            style="@style/QuoteViewText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/dashboard_qod_bg"
            android:clickable="true"
            android:focusable="true"
            android:gravity="right"
            android:padding="10dpp"
            android:textAlignment="viewEnd"
            android:textColor="#FFFFFF"
            tools:ignore="RtlCompat,RtlHardcoded" />
    </LinearLayout>
</ScrollView>

and here is the QuoteViewText style :

 <style name="QuoteViewText">
            <item name="android:textSize">100sp</item>
            <item name="android:typeface">serif</item>
            <item name="android:textStyle">bold</item>
            <item name="android:textColor">#fffcfcfc</item>
            <item name="android:shadowColor">#ff102e46</item>
            <item name="android:shadowDx">0.0</item>
            <item name="android:shadowDy">1.0</item>
            <item name="android:shadowRadius">2.0</item>
        </style>

Upvotes: 0

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

Text size is measured in sp - Scale-independent Pixels.

So, in your case you should use

<item name="android:textSize">100sp</item>

instead

<item name="android:textSize">100.0dip</item>

Also, don't forget that style is overrided by properties from your layout, in this case you should remove android:textSize from your TextView and leave it just in styles. Also you should start from a text size of 10, 100 is to big and never used.

Upvotes: 0

Er Prajesh
Er Prajesh

Reputation: 61

You are setting textview size 2 times one from applying style and another from applying textsize for the textview. Use any one. And for texts use sp instead of dp to honour user preferences.

Upvotes: 0

Related Questions