android - Programmatically created "Circle Button" not drawing -
i have custom circlebutton class:
public class circlebutton extends imageview { private int radius; private int x; private int y; public circlebutton(context context) { super(context); constructortask(); } public circlebutton(context context, attributeset attrs) { super(context, attrs); constructortask(); } public circlebutton(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); constructortask(); } public circlebutton(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); constructortask(); } private void constructortask() { x = 300; y = 300; radius = 100; } @override public void setpressed(boolean pressed) { super.setpressed(pressed); log.i("button logger","button pressed"); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawcircle(x, y, radius, gameview.green); log.i("drawing status", "circlebutton drawing..."); } } i have single activity. activity contains relative layout single custom view.
here custom view:
public class gameview extends view { public static paint green = new paint(); public gameview(context context) { super(context); green.setargb(255,0,255,0); } public gameview(context context, attributeset attrs) { super(context, attrs); green.setargb(255, 0, 255, 0); } public gameview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); green.setargb(255, 0, 255, 0); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); log.i("gameview draw status","drawing..."); main.testbutton.invalidate(); invalidate(); } } and here activity code:
public class main extends appcompatactivity { public static circlebutton testbutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); testbutton = new circlebutton(getapplicationcontext()); makefullscreen(); relativelayout screenlayout = (relativelayout) findviewbyid(r.id.screenlayout); screenlayout.addview(testbutton); } private void makefullscreen() {...} } for reason testbutton not being drawn. why not being drawn?
edit one: here xml have.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="0dp" android:paddingleft="0dp" android:paddingright="0dp" android:paddingtop="0dp" tools:context="com.example.vroy.customcirclebuttontest.main" android:id="@+id/screenlayout"> <com.example.vroy.customcirclebuttontest.gameview android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" android:id="@+id/gamescreen" /> </relativelayout> edit two: did further debugging adding normal button relative layout , worked fine.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); testcirclebutton = new circlebutton(getapplicationcontext()); makefullscreen(); testbutton = new button(getapplicationcontext()); testbutton.setx(100); testbutton.sety(100); testbutton.settext("hello world"); relativelayout screenlayout = (relativelayout) findviewbyid(r.id.screenlayout); screenlayout.addview(testcirclebutton); screenlayout.addview(testbutton); log.i("button status","adding button layout"); } for reason circlebutton not working normal button is.
you not specifying size of view (layout_width , layout_height), , view getting rendered inside 0px 0px space , invisible. can set programatically using layoutparams before adding views layout.
for example absolute size:
testbutton.setlayoutparams(new viewgroup.layoutparams(100,100)); although keep in mind difference between px , dip. want set values using internal radius attribute instead of harcoding them.
Comments
Post a Comment