Programmatic Text Wrapping in TextView

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2012-09-05T21:35:55Z Indexed on 2012/09/05 21:38 UTC
Read the original article Hit count: 214

Filed under:
|
|
|
|

I'm working on a custom widget for my application to replicate the look of a Preference button for layouts. The issue is the 'summary' text wont wrap when it hits the right wall of the view. One of the goals I'm trying to keep is that this widget is completely made from java with no xml attributes.

here's what the widget looks like at the moment:

http://dl.dropbox.com/u/56017670/Screenshot_2012-09-05-17-22-45.png

notice that the middle two don't wrap the text but the text obviously runs right off the edge of the widget

below is the code I'm using to create the text view.

public void setSummary(String summary) {
    if (!TextUtils.isEmpty(summary)) {
        if (mSummaryView == null) {
            mSummaryView = new TextView(mContext);
            mSummaryView.setTextSize(14);
            mSummaryView.setTextColor(mContext.getResources().getColor(
                                            android.R.color.tertiary_text_dark));
            addView(mSummaryView);
        }
        mSummaryView.setText(summary);
        mSummaryView.setVisibility(View.VISIBLE);
    } else {
        if (mSummaryView != null) {
            mSummaryView.setVisibility(View.GONE);
        }
    }
    mSummaryText = summary;
}

and here is the code I'm using to layout mSummaryView

mSummaryView.layout(
            mPreferencePadding + mIconWidth,
            centerVertical + (mCombinedTextHeight / 2) - mSummaryHeight,
            width - mPreferencePadding,
            centerVertical + (mCombinedTextHeight / 2));

I've tried to add

mSummaryView.setSingleLine(false);

along with quite a few other tricks but they all ended in the same way.

© Stack Overflow or respective owner

Related posts about java

Related posts about android