0

I want to add new textViews to my custom linear layout. But before the linear layout is drawn i want to check if it still fits without overlapping with the buttons on the right and if it would overlap i want to cut the text in my textView before drawing it.

my Layout

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="60dp" >

<ImageButton
    android:id="@+id/b_settings"
    android:layout_width="50dp"
    android:layout_height="44dp"
    android:layout_alignParentRight="true"
    android:layout_margin="0dp"
    android:src="@drawable/watch_settings2x" />

<Button 
    android:id="@+id/b_test"
    android:layout_width="50dp"
    android:layout_height="44dp"
    android:layout_toLeftOf="@id/b_settings"
    />

<com.client.views.Breadcrump
    android:id="@+id/ll_breadcrump"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal" />
</RelativeLayout>

my flow looks like this, first i add a new TextView to my custom View.

this.addView(TextView, position);

i have implemented the following GlobalLayoutListener which is called before the custom View is drawn, but the width is allready available:

@Override
            public void onGlobalLayout() {

                cutBreadcrump();
            }

Then i first get the available Space for my Breadcrump (custom View)

 int fragmentWidth = getView().getWidth();

         int widthButtons = mIBSettings.getWidth() + mBTest.getWidth();

         int spaceBreadcrump = fragmentWidth - widthButtons;

Next i'm cutting Chars from my new added TextView as and check the new width

CharSequence text = textView.get(0).getText();
                text = text.subSequence(0, text.length() - 1);
                textView.get(0).setText(text);

but this is my Problem. The width doesn't change after changing the Text.

mElements.get(0).getWidth();

I also tried the paint.measureText(string); method to get the width of my TextView but it was always much to long. Maybe this method does something different then what i expected ...

So my Problem is in short how to get the new width of an view after changing it's text before drawing the view

AdrianoCelentano
  • 2,461
  • 3
  • 31
  • 42
  • Using `view.getLocationInWindow(int[] coords)` should allow you to see if those views overlap. – user May 07 '13 at 11:33
  • is it propably better to get check it before the views are drawn on the screen ? – AdrianoCelentano May 07 '13 at 12:28
  • You can't do that, if the views aren't yet drawn on the screen there are no coordinates. When you add the views post a `Runnable` on a view in which you'll retrieve the coordinates with the method above. – user May 07 '13 at 12:35
  • but i can get the width of the screen and subtract the other elements. So i see how much space i have for my custom view. Before i add my new text view to my layout i can check if all textviews together fit can't i ? – AdrianoCelentano May 07 '13 at 12:38
  • So you tried doing what you said? – user May 07 '13 at 13:24
  • i think i will do it like that, i hope its the 'best' way. I will write it here when i'm done – AdrianoCelentano May 07 '13 at 14:31
  • i tried now and im stuck again, i edited my question so it still fits to the headline – AdrianoCelentano May 08 '13 at 15:14
  • Why not simply set the `Breadcrump` widget to fill the space between the left margin of the parent and the left of the *b_test* button? – user May 09 '13 at 10:48
  • that's what im doing, but when i cut the text of my textview i don't know how to change the width of the textview – AdrianoCelentano May 10 '13 at 05:58

1 Answers1

0

ok i finally did it like this i overwrote the onLayout method of my view. So when the method is called it saves the available space in a static property of my view. When i add a new TextView i check the sum of all strings with the help of this: measure text return pixels based

and the i cut my strings with a little individual algorythm till all TextViews fit in the space. afterwards i draw the TextViews.

Community
  • 1
  • 1
AdrianoCelentano
  • 2,461
  • 3
  • 31
  • 42