doplumi
doplumi

Reputation: 3118

Why TextView.setTypeface() exaggeratedly increases my TextView height when I use the Roboto-Italic.ttf font?

I'm trying to set the Roboto Light typeface on a TextView. I downloaded the Roboto fonts from here.

All I do from my code is:

sRobotoItalic = Typeface
        .createFromAsset(getContext().getAssets(), "fonts/Roboto_v1.2/Roboto-Italic.ttf");
final TextView textView = (TextView) view.findViewById(R.id.text_view);
textView.setTypeface(sRobotoItalic);

and the height of my TextView changes to be almost 5 times the height of the actual text.

I am sure it's my TextView and not some other view to change its size (have played with background colors) and that that particular typeface is causing the issue (I have tried to set different typefaces on my TextView, and nothing was wrong when I used them).

Now, I found a fix for this

sRobotoItalic = Typeface
        .createFromAsset(getContext().getAssets(), "fonts/Roboto_v1.2/Roboto-Italic.ttf");
final TextView textView = (TextView) view.findViewById(R.id.text_view);
    textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            int height = v.getHeight();
            mTypefaceUtils.setRobotoItalicTypeFace(passedExamsLabel);
            ((TextView) v).setHeight(height);
        }
    });

but it's an hack more that a solution. The code gets quickly messy if there are more text view with this same issue.

This is the TextView xml (second one)

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/marginBottom"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="@dimen/marginTop"
                android:padding="4dp">

                <TextView
                    android:id="@+id/text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="5dp"
                    android:textStyle="italic"
                    android:textSize="@dimen/text_size" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="10dp"
                    android:text="0"
                    android:textSize="@dimen/text_size"
                    android:textStyle="italic" />
            </RelativeLayout>

Am I doing something wrong? How can I repair?

Upvotes: 4

Views: 577

Answers (1)

slvn
slvn

Reputation: 1215

Ok guys, don't use the fonts provided by google but those one : https://github.com/android/platform_frameworks_base/tree/master/data/fonts

I pull the fonts from my phone (N5, 5.0) and it works well.

Upvotes: 1

Related Questions