Topper Harley
Topper Harley

Reputation: 12384

TextView don't appear as bold while they should

Here's my fragment layout:

    <RelativeLayout
        android:id="@+id/issue_main_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/issue_is_favorite"
            android:button="@drawable/favorite"
            android:padding="4dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignBottom="@+id/issue_tracker_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <net.bicou.redmine.widget.RobotoThinTextView
            android:id="@+id/issue_tracker_id"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/issue_is_private"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="32sp"/>

        <TextView
            android:id="@+id/issue_is_private"
            android:layout_toLeftOf="@+id/issue_is_favorite"
            android:layout_alignBottom="@+id/issue_tracker_id"
            android:layout_marginBottom="6dp"
            android:text="@string/issue_is_private"
            android:padding="4dp"
            android:gravity="center"
            android:visibility="gone"
            android:background="@drawable/issue_listitem_project_background_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <net.bicou.redmine.widget.RobotoCondensedLightTextView
            android:id="@+id/issue_subject"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_below="@+id/issue_is_favorite"
            android:layout_alignParentLeft="true"/>

        <ImageView
            android:id="@+id/issue_author_avatar"
            android:src="@drawable/no_avatar"
            android:layout_margin="8dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/issue_parent"
            android:layout_width="42dp"
            android:layout_height="42dp"/>

        <TextView
            android:id="@+id/issue_author"
            android:gravity="center_vertical"
            android:textColor="@color/text_light"
            android:layout_alignTop="@+id/issue_author_avatar"
            android:layout_toRightOf="@+id/issue_author_avatar"
            android:padding="4dp"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:layout_alignBottom="@+id/issue_author_avatar"/>

        <!--TODO the height of author shouldn't be hardcoded but if it's not the text is not centered vertically-->

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/issue_subject"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

        <include layout="@layout/inc_issue_overview_fields"/>

    </RelativeLayout>

</ScrollView>

At the very bottom there's a layout that is included.

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_status"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_status"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_priority"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_priority"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_assignee"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_assignee"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_category"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_category"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_target_version"/>

        <TextView
            android:id="@+id/issue_target_version"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_start_date"/>

        <TextView
            android:id="@+id/issue_start_date"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_due_date"/>

        <TextView
            android:id="@+id/issue_due_date"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_percent_done"/>

        <TextView
            android:id="@+id/issue_percent_done"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_spent_time"/>

        <TextView
            android:id="@+id/issue_spent_time"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_estimated_hours"/>

        <TextView
            android:id="@+id/issue_estimated_hours"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <View
            android:layout_weight="1"
            android:layout_span="2"
            android:layout_width="match_parent"
            android:layout_height="12dp"/>

    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:layout_weight="1"
            android:text="@string/issue_description"
            android:layout_span="2"/>

    </TableRow>

    <TableRow
        style="@style/IssueTableRow"
        android:layout_marginTop="4dp">

        <View
            android:background="#CC0000"
            android:layout_weight="1"
            android:layout_span="2"
            android:layout_width="match_parent"
            android:layout_height="2dp"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <WebView
            android:id="@+id/issue_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_span="2"/>
    </TableRow>

</TableLayout>

There's also a -sw720dp and a -sw600dp-land for 10" and 7" landscape devices.

Here's the relevant part of the style that is applied to that fragment/activity:

<style name="IssueLabel">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:layout_weight">1</item>
</style>

<style name="IssueValue">
    <item name="android:layout_weight">2</item>
</style>

That's the same, it's the default (phones) version, there's another version for 10" and 7" landscape devices.

What this defines is a basic TextView which text should be bold.
This is a custom view, so let's see how it's defined:

public class RobotoTextView extends CustomTypefaceTextView {
    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void createFont() {
        super.createFont("Roboto-Regular.ttf");
    }
}

(in another file:)

public abstract class CustomTypefaceTextView extends TextView {
    private final Context mContext;

    public CustomTypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        createFont();
    }

    public CustomTypefaceTextView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        createFont();
    }

    public CustomTypefaceTextView(final Context context) {
        super(context);
        mContext = context;
        createFont();
    }

    /**
     * This one must call super.createFont(String)
     */
    protected abstract void createFont();

    public void createFont(String typeface) {
        if (isInEditMode()) {
            return;
        }
        try {
            final Typeface font = Typeface.createFromAsset(mContext.getAssets(), typeface);
            setTypeface(font);
        } catch (final Exception e) {
        }
    }
}

Here's the overview from Android Studio (the blue highlighted part being the included layout):

Overview in Android Studio

Here's the result on a Nexus 5:

Result on a Nexus 5

As you can see, (and as the issue on the screen shows :)), the first four fields aren't bold. I'm not changing anything from the code, so I don't understand what's going on.

Any clue?


Thanks to Timothy Miko, I have fixed my CustomTypeFaceTextView#createFont(String) method as follows:

public void createFont(String typeface) {
    if (isInEditMode()) {
        return;
    }
    try {
        final Typeface font = Typeface.createFromAsset(mContext.getAssets(), typeface);
        Typeface currentFont = getTypeface();
        if (currentFont != null) {
            setTypeface(font, currentFont.getStyle());
        } else {
            setTypeface(font);
        }
    } catch (final Exception e) {
    }
}

Now it works as expected.

Upvotes: 0

Views: 299

Answers (1)

Timothy Miko
Timothy Miko

Reputation: 130

The issue is with net.bicou.redmine.widget.RobotoTextView. Instead of defining your own custom TextView, you could programmatically create the Typface and then call setTypeface() on the `TextView.

Upvotes: 1

Related Questions