1

I have 2 TextView's in a horizontal LinearLayout. The LinearLayout fills the screen horizontally. How can I achieve the following behaviour:

  1. If the first TextView is too big, it is ellipsized at the end like this:

    [firstTextViewIsTooBig...|secondTextView]

  2. If the first TextView is not big, the layout should look like this:

    [firstTextViewIs|secondTextView---------]

Important:

  • I don't know LinearLayout's width, so I can't set fixed maxWidth on the first TextView.
  • Second TextView should be always aligned to the left of the first, not to the right of the layout.

1 Answer 1

1

Look at

http://developer.android.com/reference/android/graphics/Paint.html#measureText(java.lang.String, int, int)

public float measureText (String text, int start, int end);
// Return the width of the text.

which allows you to calculate the width of a text in advance. Based on that you can adjust your layout accordingly, add the "..." ellipsis and set the width.

For the LinearLayout width, if it's FillParent, you can get the screen width, i.e. getWindow().getDecorView().getWidth(); - only after it's drawn though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.