Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For this to work you need to have a reference of those views inside your custom views classes. Something like this in <code>CustomView1</code> (same logic is applied for <code>CustomView2</code>):</p> <pre><code>public class CustomView1 extends View { byte[] bytearray = new byte[200]; private CustomView2 view2; public CustomView1(Context context, AttributeSet attrs) { super(context, attrs); } public void setCustomView2(CustomView2 view2) { this.view2 = view2; } @override onDraw() { if(view2 != null) //If view2 is not already set, we do not want a null pointer exception to occur { view2.invalidate(); } } } </code></pre> <p>To use it, you need something like this:</p> <pre><code>public class TestApp extends Activity { CustomView1 view1 ; CustomView2 view2 ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); view1 = (CustomView1) findViewbyId(R.id.CustomViewID1); view2 = (CustomView2) findViewbyID(R.id.CustomViewID2); view1.setCustomView2(view2); view2.setCustomView1(view1); } } </code></pre> <p><strong>EDIT:</strong> You can create another custom constructor for your <code>CustomView1</code> class in addition to the one you already have. Something like this:</p> <pre><code>public CustomView1(Context context) { super(context); } </code></pre> <p>This way you avoid passing a null <code>AttributeSet</code> to your view and you call your custom view like this: <code>CustomView1 view1 = new CustomView1(context)</code>. </p> <p>The <code>public CustomView1(Context context, AttributeSet attrs)</code> constructor is used to handle the situation that you declare you custom view through a layout xml file and not programmatically. You will need to set the layout properties of your view programmatically that way. Check <a href="http://startandroid.ru/en/lessons/complete-list/220-lesson-16-creating-layout-programmatically-layoutparams.html" rel="nofollow">this</a> for help in doing so.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload