0

NOTE: edited from original question where I was under the impression bounds had incorrect values.

I am trying to figure out the width of text programatically to create a textview that will contain my full text. I have the following code, setWidth() and setLayoutParams both don't seem to work.

    TextView textView = new TextView(getActivity());
    String text = "My text";
    textView.setText(text);
    Paint paint = new Paint();
    Rect bounds = new Rect();
    paint.setTypeface(textView.getTypeface());
    paint.setTextSize(textView.getTextSize());
    paint.getTextBounds(text, 0, text.length(), bounds);
    textView.setWidth(bounds.width());
    textView.setHeight(bounds.height());

Tried using LayoutParams, but still no luck.

ViewGroup.LayoutParams params =
    newViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 75);
textView.setLayoutParams(params);
wislo
  • 1,110
  • 2
  • 13
  • 24
  • I do see a lot of vague in your question do you want to insert textview created programmatically to add to your parent view right? – Vigneshwaran Murugesan Jan 22 '15 at 14:05
  • Yes. I am trying to dynamically add `TextView` objects in to a `ScrollView` and trying to adjust the width according to the text. If the text is very long, I want to potentially reduce the font size for just those objects which exceed a max width set by me. – wislo Jan 22 '15 at 14:26
  • @Vigneshearan.m please see my comment – wislo Jan 22 '15 at 19:24

2 Answers2

0

Usually such things happen when one function uses pixels, and the other uses dpi.

But why on Earth do you need to measure the text manually? Usually people specify wrap_content in the xml.

Example:

<Button
    android:text="Press me!"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="onButton1"/>

<!--
    android:layout_width="wrap_content"
-->

PS read about View layout: http://developer.android.com/reference/android/view/View.html#Layout and try to use getMeasuredWidth() and getMeasuredHeight(); if you cannot be sure that layout has already happened,use the view tree observer http://developer.android.com/reference/android/view/View.html#getViewTreeObserver() and its callbacks.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • This is for `TextViews` that I am inserting dynamically in to a `ScrollView`. However, looks like the issue is not with the bounds call. See my update to the question. – wislo Jan 22 '15 at 13:43
0

As Described Here we cannot get the bounds of the view in the OnCreateView() since the view have not been initialized you can try in OnWindowFocusChanged Method instead.

Community
  • 1
  • 1
  • I can't seem to even set the height and width for the `TextView`. When I manually set large values, I am not seeing the size change in the UI. I am adding the `TextView` to my `ScrollView` when a `ListView` item in the layout is clicked (using `onItemClick` in my Fragment which implements `AdapterView.OnItemClickListener`) – wislo Jan 22 '15 at 19:21