Hossain Khademian
Hossain Khademian

Reputation: 1696

TextView with too long Strings

I have this layout

<ScrollView>
    <TextView android:id="@+id/textView" />
</ScrollView>

When I try to set a too long text, this Textview doesn't show that.

//OnCreate
//...
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("..."); //Here is a text with more than 2500
                        //chars and at least have 10 \n char
                        //(it means has at least 10 paragraph)

How can do I show that text?

Edit One :

Even I set a background to that TextView and the TextView does'nt show that background

Upvotes: 14

Views: 30152

Answers (4)

Qilin Lou
Qilin Lou

Reputation: 406

you can set

android:maxLines="1"
android:ellipsize="end"

to your textview, so the text which long than your textview will be hide.

Upvotes: 32

ynroot
ynroot

Reputation: 79

i dont know if this will help you but saw this on developer.android...

With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView's characteristics and boundaries. This setting makes it easier to optimize the text size on different screens with dynamic content.

android:autoSizeTextType="uniform"

There are three ways you can set up the autosizing of TextView:

Default

Granularity

Preset Sizes

=================================================

readMore

Upvotes: 4

Audwin Oyong
Audwin Oyong

Reputation: 2556

Because android:singleLine="true" attribute has been deprecated, set the following attributes to your TextView:

android:ellipsize="end"
android:maxLines="1"

The TextView will then show only the text that can fit to your TextView, followed by an ellipsis ...

Upvotes: 4

Amresh
Amresh

Reputation: 2108

Use this :

  EditText thumbnailView = (EditText) findViewById(R.id.enterBearingNo_editText_id);
    TextView messageView = (TextView) findViewById(R.id.string2);
    String text = "LargeText";

    Display display = getWindowManager().getDefaultDisplay();
    FlowTextHelper.tryFlowText(text, thumbnailView, messageView, display);

FlowTextHelper.class

class FlowTextHelper {

    static boolean mNewClassAvailable;

    static {
        if (Integer.valueOf(Build.VERSION.SDK) >= 8) { // Froyo 2.2, API level 8
            mNewClassAvailable = true;
        }

        // Also you can use this trick if you don't know the exact version:
        /*
         * try {
         * Class.forName("android.text.style.LeadingMarginSpan$LeadingMarginSpan2"
         * ); mNewClassAvailable = true; } catch (Exception ex) {
         * mNewClassAvailable = false; }
         */
    }

    public static void tryFlowText(String text, View thumbnailView,
            TextView messageView, Display display) {
        // There is nothing I can do for older versions, so just return
        if (!mNewClassAvailable)
            return;

        // Get height and width of the image and height of the text line
        thumbnailView.measure(display.getWidth(), display.getHeight());
        int height = thumbnailView.getMeasuredHeight();
        int width = thumbnailView.getMeasuredWidth();
        float textLineHeight = messageView.getPaint().getTextSize();

        // Set the span according to the number of lines and width of the image
        int lines = (int) Math.round(height / textLineHeight);
        // For an html text you can use this line: SpannableStringBuilder ss =
        // (SpannableStringBuilder)Html.fromHtml(text);
        SpannableString ss = new SpannableString(text);
        ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        messageView.setText(ss);

        // Align the text with the image by removing the rule that the text is
        // to the right of the image
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) messageView
                .getLayoutParams();
        int[] rules = params.getRules();
        rules[RelativeLayout.RIGHT_OF] = 0;
    }

    public static void tryFlowTextPrice(String text, TextView messageView,
            Display display) {
        // There is nothing I can do for older versions, so just return
        if (!mNewClassAvailable)
            return;

        // Get height and width of the image and height of the text line
        // thumbnailView.measure(display.getWidth(), display.getHeight());
        // int height = thumbnailView.getMeasuredHeight();
        // int width = thumbnailView.getMeasuredWidth();
        float textLineHeight = messageView.getPaint().getTextSize();

        // Set the span according to the number of lines and width of the image
        // int lines = (int) Math.round(height / textLineHeight);
        // For an html text you can use this line: SpannableStringBuilder ss =
        // (SpannableStringBuilder)Html.fromHtml(text);
        SpannableString ss = new SpannableString(text);
        // ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
        // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        messageView.setText(ss);

        // Align the text with the image by removing the rule that the text is
        // to the right of the image
        // LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
        // messageView
        // .getLayoutParams();
        // int[] rules = params.getRules();
        // rules[RelativeLayout.RIGHT_OF] = 0;
    }

}

Upvotes: 2

Related Questions