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);