3

I want to get height of linear layout that is set to wrap content. but it is zero. i try this codes:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);


         ViewTreeObserver vto = below.getViewTreeObserver();  
         vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
             @Override  
             public void onGlobalLayout() {  
                  width  = below.getMeasuredWidth(); 
                  height = below.getMeasuredHeight();  

             }  
         });

and :

 LinearLayout below= (LinearLayout) findViewById(R.id.below);
       final int belowh=below.getHeight();

using code:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);
        ViewTreeObserver vto = below.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() { 

        if(below.getMeasuredHeight()> 0){

                 this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                int width  = below.getMeasuredWidth();
                int height = below.getMeasuredHeight(); 
        }
            } 
        })

    ;

click here to see

Linh
  • 57,942
  • 23
  • 262
  • 279
coder android
  • 83
  • 1
  • 7

3 Answers3

6

Try this code for getting layout width and height after your view is created

final LinearLayout below= (LinearLayout) findViewById(R.id.below);
    below.post(new Runnable() 
        {
            @Override
            public void run()
            {
                Log.i("TAG", "Layout width :"+ below.getWidth());
                Log.i("TAG", "Layout height :"+ below.getHeight());
            }
    });

Hope this help

Linh
  • 57,942
  • 23
  • 262
  • 279
  • your code works well and also @Md. Shahadat Sarker segustion works-call it after the view has been laid out- but i don't know which one is better. – coder android Feb 01 '16 at 05:05
2

Try this

final LinearLayout below= (LinearLayout) findViewById(R.id.below);
    ViewTreeObserver vto = below.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 

    if(below.getMeasuredHeight()> 0){

             this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            int width  = below.getMeasuredWidth();
            int height = below.getMeasuredHeight(); 
    }
        } 
    })

;
Mobile Apps Expert
  • 1,028
  • 1
  • 8
  • 11
0

@coder android

If you still stack on this.... please see this. I think this will work perfectly.

The following code is found How to retrieve the dimensions of a view?

final TextView tv = (TextView)findViewById(R.id.image_test);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        LayerDrawable ld = (LayerDrawable)tv.getBackground();
        ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);
        ViewTreeObserver obs = tv.getViewTreeObserver();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }
    }

});
Community
  • 1
  • 1