android - how i can get width and height dimension from my customview? -
i have custom view ...
package nan.salsa.goal.customview; import android.r; import android.content.context; import android.graphics.canvas; import android.graphics.drawable.shapedrawable; import android.graphics.drawable.shapes.rectshape; import android.util.attributeset; import android.util.log; import android.view.view; public class dayview extends view { private static string tag="dayview"; private shapedrawable mdrawable; public dayview(context context) { super(context); } public dayview(context context, attributeset attrs) { super(context, attrs); init(); } public dayview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } public void init() { int x = 10; int y = 10; int width = 300; int height = 300; mdrawable = new shapedrawable(new rectshape()); mdrawable.getpaint().setcolor(0xff74ac23); mdrawable.setbounds(x, y, x + width, y + height); } @override protected void ondraw(canvas canvas) { // todo auto-generated method stub super.ondraw(canvas); setbackgroundcolor(r.color.black); mdrawable.draw(canvas); } }
with simple configuration file :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#e06f00"> <nan.salsa.goal.customview.dayview android:id="@+id/dayview" android:layout_height="match_parent" android:layout_width="fill_parent" /> </linearlayout>
in view want dinamically set width of rectangle ...
i have tried :
view parent = getparent(); int width = parent.getwidth();
// throw nullpointerexception because parent null ...
and also:
getrootview().getwidth();
// in case no null pointerexception method return 0:
how can ?
regards
antonio musella
by adding method custom view class:
@override public void onsizechanged (int w, int h, int oldw, int oldh){ super.onsizechanged(w, h, oldw, oldh); screenw = w; screenh = h; }
Comments
Post a Comment