21

I have TextView created programmatically, like that:

                TextView mTextView = new TextView(getApplicationContext());

                final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                ((MarginLayoutParams) params).setMargins(8, 8, 0, 0);

                mTextView.setText(mText);
                mTextView.setLayoutParams(params);
                mTextView.setPadding(2, 2, 2, 2);
                mTextView.setBackground(getResources().getDrawable(R.drawable.note_corps));

                tagMap.addView(mTextView);

                textViewsWidth = mTextView.getWidth();

But mTextView.getWidth() always returns 0

And if I try:

mTextView.getLayoutParams().width

It returns the LayoutParams corresponding value in the LayoutParams class (-1 or -2)

How can I get the view's width ?

EDIT I need to do this here:

            @Override
        public void onPostExecute(Hashtable<String, Integer> hash){
            final ScrollView tagMapScroll = (ScrollView) findViewById(R.id.tagMapScroll);
            final LinearLayout tagMap = (LinearLayout) findViewById(R.id.tagMap);
            final ArrayList<Integer> arr = new ArrayList<Integer>(hash.values());
            final Enumeration<String> e = hash.keys();
            int index = 0;
            int textViewsWidth = 0;

            while(e.hasMoreElements()){
                final TextView tV = new TextView(getApplicationContext());

                tV.setTextColor(Color.parseColor("#666666"));
                tV.setText(Html.fromHtml(randomColor() + e.nextElement()));
                tV.setTextSize(arr.get(index));

                final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                ((MarginLayoutParams) params).setMargins(8, 8, 0, 0);

                tV.setLayoutParams(params);
                tV.setPadding(2, 2, 2, 2);
                tV.setBackground(getResources().getDrawable(R.drawable.note_corps));

                tagMap.addView(tV);

                textViewsWidth += tV.getWidth();

                index++;
            }

            tagMapScroll.setVisibility(ScrollView.VISIBLE);

        }

EDIT SOLUTION I used this from @androiduser's answer:

mTextView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                int width = mTextView.getMeasuredWidth();
                int height = mTextView.getMeasuredHeight();

Problem solved !

Pumpkin
  • 319
  • 2
  • 3
  • 11
  • 1
    You cannot get the width of the view, till the `onMeasure` has been called on the view. Refer: http://stackoverflow.com/q/3591784/827110 – Amulya Khare Oct 28 '13 at 11:21
  • Now if you tell us what you need the width for? Maybe someone can propose a solution.. – Amulya Khare Oct 28 '13 at 11:22
  • You are calling getWidth() too early. The UI has not been sized and laid out on the screen yet. – Siddharth_Vyas Oct 28 '13 at 11:22
  • If any answer was helpful please mark as correct answer to close the thread :D – Diego Palomar Oct 28 '13 at 11:29
  • edited, I need to do this for multiples TextView (probably hundreds) and in an onPostExecute() – Pumpkin Oct 28 '13 at 11:39
  • I tried to reach the gone view's height in onPost method, I tried adding OnGlobalLayoutListener but my gone view's height keep getting 0. But your solution is work like a charm. But folks, for knowledge on this topic you can read these answers: [1] https://stackoverflow.com/questions/4680499/how-to-get-the-width-and-height-of-an-android-widget-imageview/ [2] https://stackoverflow.com/questions/4712796/android-get-viewheight-programmatically – Eren Utku Nov 18 '17 at 10:23

5 Answers5

17

I used this solution:

yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        // Ensure you call it only once
        yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

        // Here you can get the size :)
    }
});
Kedar Tendolkar
  • 474
  • 3
  • 9
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
4

get this way:

tV.post(new Runnable() {
                    @Override
                    public void run() {
                        int width=tV.getMeasuredWidth();
                    }
                });
Shivang Trivedi
  • 2,182
  • 1
  • 20
  • 26
2

those values are constant value for FILL_PARENT and WRAP_CONTENT. If you want to check the view size you can try this way:

tagMap.addView(mTextView);
tagMap.post(new Runnable() {
                    @Override
                    public void run() {
                        mTextView. getWidth();
                    }
                });

you have to wait until android draws the TextView. This way you are posting a Runnable in the tagMap queue, that is executed, hopefully after the textview is draw (so it should be weight and height)

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I need to get the width right after I added the view to the tagMap, but I don't get it, when I make a toast it's still 0 – Pumpkin Oct 28 '13 at 12:06
1

In this method you can get the width height of the view..

    @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    int width = mTextView.getWidth();
    int height = mTextView.getHeight();
}

and define TextView mTextView as global

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
0

You have to use ViewTreeObserver class which is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.

In the Activity's onCreate mehotd put this:

ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        int viewWidth = mTextView.getMeasuredWidth();

    }
});
Diego Palomar
  • 6,958
  • 2
  • 31
  • 42