6

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

LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);

public static void main(String[] args)
{
    DBFacade dbFacade = new DBFacade();
    dbFacade.pick();
}

//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;
iknow
  • 8,358
  • 12
  • 41
  • 68
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

6 Answers6

13

Probably you are calling getWidth() and getHeight() too early: I think the UI has not been sized and laid out on the screen yet...
You can try to put that code inside this:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    // Call here getWidth() and getHeight()
 }
Marco
  • 56,740
  • 14
  • 129
  • 152
  • so when i should call these function.I have gone through many examples but cant get the answer..can u please help me to solve these out – AndroidDev Oct 13 '11 at 06:17
  • @Anshuman: create dinamically layout and let it show before checking width and height. In my edited code I provided an example, but I'm sure it's not the only one... – Marco Oct 13 '11 at 06:18
  • @Anshuman: you didn't tell us in which function/event you are calling your code... – Marco Oct 13 '11 at 06:19
  • Yeh when i write the code for getWidth and getHeight on onWindowFocusChanged(boolean hasFocus) and gives me the result.And is it true that for different Resolution it gives different width and height – AndroidDev Oct 13 '11 at 06:30
  • Thanks; this fixed the problem. (Calling methods too early) Try to set getWidth after you call `show` on the `Scene` – Don Larynx Aug 22 '15 at 22:59
  • Aha, that is the answer! Thanks. – driftwood Apr 23 '18 at 03:31
7

Another way

ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {

        width = button.getWidth();
        height = button.getHeight();
    }
});
Linh
  • 57,942
  • 23
  • 262
  • 279
Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40
1

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 it

Jigar Patel
  • 436
  • 4
  • 9
0

You can override the following View method, called during the layout phase.

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

This seems more appropriate, as it expressly accounts for the zero width and height you're observing:

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Parameters w Current width of this view. h Current height of this view. oldw Old width of this view. oldh Old height of this view.

See here.

Ken
  • 30,811
  • 34
  • 116
  • 155
0

You try to use the count-variable "i" and "j" to use outside from both for-loops. That should raise an exception because the are only availabil in each for-block.

Try to make the output in the second for-loop...

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));
    }
}
Marc Juschkeit
  • 576
  • 5
  • 6
-1

Try this can also work:

btn.getDrawingCache().getHeight() and btn.getDrawingCache().getWidth()
mayank_droid
  • 1,015
  • 10
  • 19