9

I am trying to add a text to the textview for which i have set the width as Wrap_content. I am trying to get the width of this textview. But its showing 0 in all the cases. How Can i get the width of the textview after setting the text into it.

the code is as:

        LinearLayout ll= new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        TextView tv = new TextView(this);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        ll.addView(tv);
        tv.setText("Hello 1234567890-0987654321qwertyuio787888888888888888888888888888888888888888888888888");
        System.out.println("The width is == "+tv.getWidth());// result is 0
        this.setContentView(ll);

Please suggest. Thanks in advance.

Aada
  • 1,591
  • 7
  • 30
  • 57

8 Answers8

11

Views with the dynamic width/height get their correct size only after a layout process was finished (http://developer.android.com/reference/android/view/View.html#Layout).
You can add OnLayoutChangeListener to your TextView and get it's size there:

tv.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
           public void onLayoutChange(View v, int left, int top, int right, int bottom, 
                                      int oldLeft, int oldTop, int oldRight, int oldBottom) {
                        final int width = right - left;
                        System.out.println("The width is == " + width);                
    });
Const
  • 976
  • 7
  • 11
3

You can not get the width of a View with dynamic size before the layout is completely built. That means there is no way you can get it in onCreate(). One way would be to create a class that inherits from TextView and overrides onSizeChanged().

SimonSays
  • 10,867
  • 7
  • 44
  • 59
1

When are you calling this? Has it already been drawn to the screen?

It sounds like you are calling getWidth() too early.

You can also take a look at this question.

Community
  • 1
  • 1
Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
0

You should use this:

textView.getMeasuredWidth();

Steven.Nguyen
  • 1,034
  • 13
  • 14
0

this works for me:

RelativeLayout.LayoutParams mTextViewLayoutParams = (RelativeLayout.LayoutParams) mTextView.getLayoutParams();
mTextView.setText(R.string.text);
mTextView.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int width = mShareTip.getMeasuredWidth();
//use the width to do what you want
mShareTip.setLayoutParams(mShareTipLayoutParams);
shuo Han
  • 79
  • 6
0

you can try this:

textView.measure(0,0);
int width = textView.getMeasuredWidth();
Robert Lee
  • 21
  • 2
0

hello use this method:

textview.post(new Runnable() {
    @Override
    public void run() {
        int width = textview.getWidth();
        int height = textview.getHeight();
        textview.setText( String.valueOf( width +","+ height ));
    }

});

source: https://gist.github.com/omorandi/59e8b06a6e81d4b8364f

سعید .م
  • 137
  • 1
  • 8
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Mogsdad Dec 20 '17 at 20:30
0

You can use this library to schedule the task of perform calculation on the width to the correct time after the view had been completely drawn

https://github.com/Mohamed-Fadel/MainThreadScheduler

Sample of use:

MainThreadScheduler.scheduleWhenIdle(new Runnable() {
       @Override
       public void run() {
           int width = textview.getWidth();
           int height = textview.getHeight();
           textview.setText( String.valueOf( width +","+ height ));
       }
   });