tags:

views:

62

answers:

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  A: 

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.

Mathias Lin