How to get the height and width of a button

I created an array of buttons. Now I want to find the height and width of the button, and for this I used getWidth () and getHeight (). But the fact is that it always returns 0. Why is this happening? I am posting my code, please check if something is wrong.

int x,y; LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout); LinearLayout rowLayout = null; LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1); //Create Button for (int i = 0; i<6; i++) { rowLayout = new LinearLayout(this); rowLayout.setWeightSum(7); layoutVertical.addView(rowLayout, param); for(int j=0; j<7; j++) { m_pBtnDay[i][j] = new Button(this); rowLayout.addView(m_pBtnDay[i][j], param); m_pBtnDay[i][j].setOnLongClickListener(this); m_pBtnDay[i][j].setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); m_pBtnDay[i][j].setTextSize(12); } } x = m_pBtnDay[i][j].getWidth(); y = m_pBtnDay[i][j].getHeight(); Log.d("width", Integer.toString(x)); Log.d("Height", Integer.toString(y)); return true; 
+3
android
source share
6 answers

You are probably calling getWidth() and getHeight() too early: I think the user interface has not yet been measured and laid out on the screen ...
You can try putting this code in this:

 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); // Call here getWidth() and getHeight() } 
+11
source share

Another way

 ViewTreeObserver vto = button.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { width = button.getWidth(); height = button.getHeight(); } }); 
+7
source share

put this in your loop

 x = m_pBtnDay[i][j].getWidth(); y = m_pBtnDay[i][j].getHeight(); Log.d("width", Integer.toString(x)); Log.d("Height", Integer.toString(y)); 

try

+1
source share

You are trying to use count-variable "i" and "j" to use externally as for-loops. This should throw an exception, because they are available only for each block.

Try to make a conclusion in the second cycle ...

 for (int i = 0; i<6; i++) { ... for(int j=0; j<7; j++) { .... x = m_pBtnDay[i][j].getWidth(); y = m_pBtnDay[i][j].getHeight(); Log.d("width", Integer.toString(x)); Log.d("Height", Integer.toString(y)); } } 
0
source share

You can override the following View method, which is called during the build phase.

 protected void onSizeChanged (int w, int h, int oldw, int oldh) 

This seems more appropriate since it explicitly takes into account the zero width and height that you are observing:

This is called at build time when the size of this view has changed. If you are simply added to the view hierarchy, you are invoked using the old values โ€‹โ€‹of 0.

Parameters w Current width of this view. h The current height of this view. oldw The old width of this view. oldh The old height of this view.

See here .

0
source share

Try this also:

 btn.getDrawingCache().getHeight() and btn.getDrawingCache().getWidth() 
-one
source share

All Articles