android - Custom view in the second row of a LinearLayout not getting displayed -


/* code snipet mainactivity */ @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     /* linear layout  */     linearlayout layout = (linearlayout)findviewbyid(r.id.main_container);      /* creating 2 similar custom views */     customview row1 = new customview(this);  /* first row */     customview row2 = new customview(this);  /* second row */      layout.add(row1,0);     layout.add(row2,1); } 

customview.java

public class customview extends view {      public final string tag = "customview";     context context;      private drawable backgrounddrawable;      rect backgrounddrawablerect;      public textmessageview(context context) {         super(context);         this.context = context;         backgrounddrawable = contextcompat.getdrawable(context,r.drawable.background_drawable);     }      @override     protected void onlayout(boolean changed, int left, int top, int right, int bottom) {          super.onlayout(changed, left, top, right, bottom);          backgrounddrawablerect.left = left;         backgrounddrawablerect.right = right;         backgrounddrawablerect.bottom = bottom;         backgrounddrawablerect.top = top;          backgrounddrawable.setbounds(backgrounddrawablerect);     }      @override     protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {         /* 100 pixel height view */         setmeasureddimension(measurespec.getsize(widthmeasurespec),100);     }      @override     protected void ondraw(canvas canvas) {          super.ondraw(canvas);          if(backgrounddrawable != null) {             backgrounddrawable.draw(canvas);         }     }  } 

activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:id="@+id/main_container"     android:layout_width="match_parent"     android:layout_height="match_parent">  </linearlayout> 

the problem view added in second row of linear layout not getting displayed. during debugging, when enabled 'show layout bound ' in developers option , found view in second row taking required space content drawable not getting displayed.

the canvas coordinates automatically adjusted view's position need set left , top bounds of drawable 0 instead:

backgrounddrawablerect.left = 0; backgrounddrawablerect.right = right - left; backgrounddrawablerect.bottom = bottom - top; backgrounddrawablerect.top = 0; 

Comments