I want to use getWidth()/getHeight() to get width/height of my XML-Layout. I read I have to do it in the method onSizeChanged() otherwise I will get 0 ( Android: Get the scr
What I would recommend doing is extending ListView in your own custom class. Define a class called MyListView, or something similar, and make sure you define all three constructors. Then, override the onSizeChanged method to call something externally - much like an OnClickListener or OnTouchListener. You could define a method in MyListView to accept a listener, instantiate a listener in your activity, and then when onSizeChanged is called, pass it on through to the listener. This is really hard to explain in English. Here is some sample code:
Custom ListView:
public class MyListView extends ListView
{
public MyListView(Context context)
{
super(context);
}
public MyListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public void SetOnResizeListener(MyOnResizeListener orlExt)
{
orl = orlExt;
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
super.onSizeChanged(xNew, yNew, xOld, yOld);
if(orl != null)
{
orl.OnResize(this.getId(), xNew, yNew, xOld, yOld);
}
}
MyOnResizeListener orl = null;
}
Custom listener:
public class MyOnResizeListener
{
public MyOnResizeListener(){}
public void OnResize(int id, int xNew, int yNew, int xOld, int yOld){}
}
You instantiate the listener like:
Class MyActivity extends Activity
{
/***Stuff***/
MyOnResizeListener orlResized = new MyOnResizeListener()
{
@Override
public void OnResize(int id, int xNew, int yNew, int xOld, int yOld)
{
/***Handle resize event****/
}
};
}
And don't forget to pass your listener to your custom view:
/***Probably in your activity's onCreate***/
((MyListView)findViewById(R.id.MyList)).SetOnResizeListener(orlResized);
Finally, you can add your custom list to an XML layout by doing something like:
<com.myapplication.whatever.MyListView>
<!-- Attributes -->
<com.myapplication.whatever.MyListView/>
Just add a method to your custom view that you call when onSizeChanged takes place in the Activity. Pass the new values to the view as parameters of the called method. Then execute whatever operations need to take place when your custom view changes size.
i think i know what you want to know........... i just wrote a new blog post on that right now........... how to get width and height dimensions of a customView (extends View) in Android http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html
You dont have to create a customView
to get its height and width. You can add an OnLayoutChangedListener (description here) to the view whose width/height you want, and then essentially get the values in the onLayoutChanged method, like so
View myView = findViewById(R.id.my_view);
myView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
// its possible that the layout is not complete in which case
// we will get all zero values for the positions, so ignore the event
if (left == 0 && top == 0 && right == 0 && bottom == 0) {
return;
}
// Do what you need to do with the height/width since they are now set
}
});
The reason for this is because views are drawn only after the layout is complete. The system then walks down the view heirarchy tree to measure the width/height of each view before drawing them.
I had a similar problem (ie. calculating View
sizes in an Activity
after it was finished drawing).
I overrode the Activity
method : public void onWindowFocusChanged(boolean hasFocus)
and it worked fine.