metter
metter

Reputation: 1442

Android layout: Measure views in order to choose correct TextView font size

I am currently working on an Activity that features components with weight. Those components contain text and icons. As far as I know, android does not provide features to scale text according to it's parent view. As a consequence, I need to measure those views and then manually apply my custom font to the TextViews and set an appropriate font size.

I am currently doing it the following way (Which actually works. But I often get 09-30 17:55:14.844: ERROR/ViewRoot(12893): OutOfResourcesException locking surface ) errors and I believe this might be connected to the way I do my layout.

This is one row in the layout (there are several rows)

<RelativeLayout 
    android:layout_height="0dip" 
    android:layout_width="fill_parent" 
    android:layout_weight="0.35"
    android:id="@+id/activity_main_row_1">
    <View
        android:layout_width="6dip"
        android:layout_height="fill_parent"
        android:background="@color/palette_green"
        android:layout_alignParentLeft="true"
    />
    <RelativeLayout 
        android:layout_centerVertical="true"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:paddingLeft="20dip">
        <TextView 
            android:id="@+id/activity_main_row_1_title" 
            android:text="@string/title_add_expense" 
            android:textSize="10dip"
            android:textColor="@color/palette_grey"
            android:layout_alignParentLeft="true" 
            android:layout_alignParentTop="true" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="0px">
        </TextView>
        <TextView 
            android:id="@+id/activity_main_row_1_sub_title" 
            android:text="@string/subtitle_add_expense" 
            android:textSize="10dip"
            android:textColor="@color/palette_dark_grey"
            android:layout_alignParentLeft="true" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_margin="0px"
            android:layout_below="@+id/activity_main_row_1_title">
        </TextView>
    </RelativeLayout>
    <ImageView 
        android:id="@+id/activity_main_row_1_bracket" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon_bracket_right_grey" 
        android:paddingLeft="0dip"
        android:paddingRight="0dip"
        android:paddingTop="24dip"
        android:paddingBottom="20dip"
        android:layout_alignParentRight="true">
    </ImageView>

</RelativeLayout>

This is the way I do the measurements:

in onCreate:

LinearLayout mLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.activity_main, null);
    mLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
    this.setContentView(mLayout);

In onGlobalLayout:

@Override
public void onGlobalLayout() {

    int mRow1Height = mRow1.getHeight();
    <omitted>


    if(mRow1Height>0) {
        TextView row1Title = (TextView) findViewById(R.id.activity_main_row_1_title);
        row1Title.setTypeface(mFontProvider.getTypeface());
        row1Title.setTextSize((float) ((mRow1Height)*.3));

        TextView row1SubTitle = (TextView) findViewById(R.id.activity_main_row_1_sub_title);
        row1SubTitle.setTypeface(mFontProvider.getTypeface());
        row1SubTitle.setTextSize((float) ((mRow1Height)*.2));
    }

Is this the correct way to do what I want to do?

Thanks so much for your advice.

Cheers

Upvotes: 0

Views: 564

Answers (1)

metter
metter

Reputation: 1442

I found the issue that was causing the OutOfResourcesExceptions. In the onLayout, I set the text for a TextView. This causes the view to be laid out again, resulting in kind of an an infinite loop.

I'd however still be interested in knowing if using addOnGlobalLayoutListener and onLayout is the only way to scale TextViews in respect to their dynamically sized parent views.

Upvotes: 1

Related Questions